【问题标题】:Need a macro to search and update the record from one worksheet to another需要一个宏来搜索记录并将其从一个工作表更新到另一个工作表
【发布时间】:2015-03-07 15:35:14
【问题描述】:

我需要一个宏代码来搜索单元格 1 到 1000 中 sheet1 中的内容(可能是数字或字母数字),并从 sheet2 中搜索相同的文本。如果找到了,那么我需要更新与相邻单元格对应的内容。 例如:

sheet1
1024     D
505A
6057     C




sheet2
1024     D
6057     C

【问题讨论】:

  • 那么除了第二列中没有任何内容的行(没有 D 或 C 等)已被删除之外,工作表 2 是否最终看起来像工作表 1?或者您是说 Sheet 2 一开始在第 2 列中没有字母,而您的宏将它找到的任何字母放在第 1 表中的 1024 旁边,在第 2 表中的 1024 旁边?

标签: vba excel excel-2010


【解决方案1】:

假设查找值在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多