Need a macro to search and update the record from one worksheet to another

a 夏天 提交于 2019-12-13 09:54:41

问题


I need a macro code to search the content(it may be numeric or aplhanumeric) from sheet1 in the cell 1 to 1000 and to search the same text from sheet2. and if it founds then i need to update the content corresponding to adjacent cell. eg:

sheet1
1024     D
505A
6057     C




sheet2
1024     D
6057     C

回答1:


Assuming the look up values are in column A and the value to copy is in column 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


来源:https://stackoverflow.com/questions/28916540/need-a-macro-to-search-and-update-the-record-from-one-worksheet-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!