Vba 需要宏从一个工作表到另一个工作表搜索和更新记录
我需要一个宏代码来搜索单元格1到1000中sheet1中的内容(可能是数字或数字),并搜索sheet2中的相同文本。如果找到了,我需要更新与相邻单元格对应的内容。 例如:Vba 需要宏从一个工作表到另一个工作表搜索和更新记录,vba,excel,excel-2010,Vba,Excel,Excel 2010,我需要一个宏代码来搜索单元格1到1000中sheet1中的内容(可能是数字或数字),并搜索sheet2中的相同文本。如果找到了,我需要更新与相邻单元格对应的内容。 例如: 假设查找值在A列中,要复制的值在B列中 Sub FindValues() Dim lookUpSheet As Worksheet, updateSheet As Worksheet Dim valueToSearch As String Dim i As Integer, t As Integer Set lookUpS
假设查找值在A列中,要复制的值在B列中
Sub FindValues()
Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer
Set lookUpSheet = Worksheets("sheet1")
Set updateSheet = Worksheets("sheet2")
'get the number of the last row with data in sheet1 and in sheet2
lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row
'for every value in column A of sheet2
For i = 1 To lastRowUpdate
valueToSearch = updateSheet.Cells(i, 1)
'look the value in column A of sheet1
For t = 1 To lastRowLookup
'if found a match, copy column B value to sheet1 and proceed to the next value
If lookUpSheet.Cells(t, 1) = valueToSearch Then
updateSheet.Cells(i, 2) = lookUpSheet.Cells(t, 2)
Exit For
End If
Next t
Next i
End Sub
那么,除了删除了第2列中没有任何内容(没有D、C等)的行之外,表2最终是否看起来像表1?或者,您是说第2页开始时第2列中没有字母,而您的宏将在第1页的1024旁边找到的字母放在第2页的1024旁边?
Sub FindValues()
Dim lookUpSheet As Worksheet, updateSheet As Worksheet
Dim valueToSearch As String
Dim i As Integer, t As Integer
Set lookUpSheet = Worksheets("sheet1")
Set updateSheet = Worksheets("sheet2")
'get the number of the last row with data in sheet1 and in sheet2
lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
lastRowUpdate = updateSheet.Cells(Rows.Count, "A").End(xlUp).Row
'for every value in column A of sheet2
For i = 1 To lastRowUpdate
valueToSearch = updateSheet.Cells(i, 1)
'look the value in column A of sheet1
For t = 1 To lastRowLookup
'if found a match, copy column B value to sheet1 and proceed to the next value
If lookUpSheet.Cells(t, 1) = valueToSearch Then
updateSheet.Cells(i, 2) = lookUpSheet.Cells(t, 2)
Exit For
End If
Next t
Next i
End Sub