excel sheet1的a列内容查找sheet2的a列内容找到把对应的b列复制到sheet1的b列。sheet2的a列内容会有多条。vba实现
以下是VBA代码实现该功能的示例:
Sub CopyData()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim i As Long
Dim j As Long
' 设置要操作的工作表
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' 获取Sheet1和Sheet2的最后一行行号
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
' 遍历Sheet1的A列
For i = 1 To lastRow1
' 遍历Sheet2的A列
For j = 1 To lastRow2
' 如果Sheet1的A列值等于Sheet2的A列值,则复制Sheet2的B列值到Sheet1的B列
If ws1.Cells(i, "A").Value = ws2.Cells(j, "A").Value Then
ws1.Cells(i, "B").Value = ws2.Cells(j, "B").Value
Exit For ' 找到匹配项后退出内层循环
End If
Next j
Next i
' 清除对象引用
Set ws1 = Nothing
Set ws2 = Nothing
End Sub
请注意,在运行该代码之前,请将Sheet1和Sheet2替换为您实际使用的工作表名称
原文地址: https://www.cveoy.top/t/topic/iqo9 著作权归作者所有。请勿转载和采集!