VBA VlLOOKUP 将sheet1 中AB两列与Sheet2中AB两列进行匹配,复制C列相同的值到Sheet1C列中
时间: 2023-06-04 17:03:30 浏览: 147
Excel VBA 根据Sheet2中的表格数据处理Sheet1中的数据,包括比较、复制
可以,这是一个VBA代码问题。以下是示例代码:
Sub vlookup()
Dim sheet1 As Worksheet
Dim sheet2 As Worksheet
Set sheet1 = ThisWorkbook.Worksheets("Sheet1")
Set sheet2 = ThisWorkbook.Worksheets("Sheet2")
Dim lastRow As Long
lastRow = sheet1.Cells(sheet1.Rows.Count, "A").End(xlUp).Row
Dim lookupValue As Variant
Dim lookupRange As Variant
Dim columnNumber As Long
Dim matchType As Boolean
lookupValue = sheet1.Range("A2:A" & lastRow)
lookupRange = sheet2.Range("A:B")
columnNumber = 2
matchType = False
For i = LBound(lookupValue) To UBound(lookupValue)
sheet1.Cells(i + 1, 3) = Application.VLookup(lookupValue(i, 1), lookupRange, columnNumber, matchType)
Next i
End Sub
请将代码粘贴到VBA编辑器中,并将Sheet1和Sheet2名称替换为您实际使用的名称。注意,此代码假定Sheet1和Sheet2中的数据都已经按AB列排序。如果未排序,您需要添加额外的代码来排序数据。
阅读全文