excel模式为:
sheet1:
sheet2:
然后选择 工具—宏—vb编译器:加入模块 写入代码:
Sub zxl_input_phone()
Set sh2 = Worksheets("Sheet2") '将sheet2工作表用对象变量表示
r2 = sh2.Range("A65536").End(xlUp).Row '求sheet2工作表A列数据最大行号
Worksheets("Sheet1").Activate '激活sheet1工作表
r1 = Range("A65536").End(xlUp).Row '求sheet1工作表A列数据最大行号
For I = 2 To r1 '对sheet1工作表进行循环
xm1 = Replace(Cells(I, 1), " ", "") '把姓名复制给变量xm1(去掉空格)
For J = 2 To r2 '对sheet2工作表进行循环
xm2 = Replace(sh2.Cells(J, 1), " ", "") '把姓名赋值给变量xm2(去掉空格)
If xm2 = xm1 Then '姓名相同
Cells(I, 2) = sh2.Cells(J, 2) '导入对应电话号码
Exit For
End If
Next
Next
End Sub