写一个VBA代码,实现以下需求,1.如果表1的F列与表3的A列值匹配一致,则将对应行表1的E列值返回给表3的B列值
时间: 2024-03-03 17:48:03 浏览: 58
以下是实现你要求的VBA代码:
```VBA
Sub MatchAndUpdate()
Dim ws1 As Worksheet, ws3 As Worksheet
Dim lastRow1 As Long, lastRow3 As Long
Dim i As Long, j As Long
'指定要操作的表格
Set ws1 = ThisWorkbook.Worksheets("表1")
Set ws3 = ThisWorkbook.Worksheets("表3")
'获取表格1和表格3的最后一行
lastRow1 = ws1.Cells(ws1.Rows.Count, "F").End(xlUp).Row
lastRow3 = ws3.Cells(ws3.Rows.Count, "A").End(xlUp).Row
'循环比较表格1和表格3的A列和F列的值,如果匹配则更新表格3的B列
For i = 1 To lastRow3
For j = 1 To lastRow1
If ws3.Cells(i, "A").Value = ws1.Cells(j, "F").Value Then
ws3.Cells(i, "B").Value = ws1.Cells(j, "E").Value
Exit For '找到匹配就退出内层循环
End If
Next j
Next i
End Sub
```
注意:在使用此代码之前,请先将代码中的表格名称修改为你实际使用的表格名称。另外,为了保证代码的正确性,在使用之前,请务必备份你的数据。
阅读全文