简体   繁体   English

Excel宏在多列中查找

[英]Excel Macro Find in multiple columns

I have two worksheets "Accounts" and "Mock up". 我有两个工作表“帐户”和“模拟”。 In the first sheet, I have columns A to L and 1409 rows. 在第一张纸上,我有A到L列和1409行。 In the second sheet, the user enters a value on cell B4. 在第二页中,用户在单元格B4上输入一个值。 I have to search for this value in the range A1:A1409 in the first sheet. 我必须在第一张纸的A1:A1409范围内搜索该值。 If the value is found in A100, then A100 to L100 will be assigned to cells B8:B11,B16:B19 and B22:B25 in the second sheet. 如果在A100中找到该值,则将A100到L100分配给第二张工作表中的单元格B8:B11,B16:B19和B22:B25。

If it is not found in the range A1:A1409, then I have to search in the range E1:E1409 and return A100 to L100 again to B8:B11,B16:B19 and B22:B25 in the second sheet. 如果在范围A1:A1409中找不到它,那么我必须在范围E1:E1409中进行搜索,然后将A100再次返回L100到第二张纸中的B8:B11,B16:B19和B22:B25。

Trying to create a excel 2010 vba macro to accomplish this. 尝试创建一个excel 2010 vba宏来完成此操作。 Any help is appreciated. 任何帮助表示赞赏。

1st Sheet 第一张

在此处输入图片说明

2nd sheet 第二张

第二张

Regards, Ragav. 问候,拉加夫。

What happens in this code: 这段代码会发生什么:

  • Take the search value from "Mock Up!B4" 从“模拟!B4”中获取搜索值
  • Search through column A on "Accounts" 在“帐户”上的A列中搜索
  • If a match is found, copy the data from "Mock Up" to "Accounts"(row), matching the pictures provided. 如果找到匹配项,则将数据从“模拟”复制到“帐户”(行),以匹配提供的图片。
  • If no match found in "A", search again through "E" 如果在“ A”中找不到匹配项,请通过“ E”再次搜索
  • If match found on "E", copy the data from "Accounts"(row), to "Mock Up" 如果在“ E”上找到匹配项,则将数据从“帐户”(行)复制到“模拟”

Code: 码:

Sub MockUpTranfer()
Dim lastRow As Long, lRow As Long
Dim source As String, target As String, tempVal As String
Dim match As Boolean

    match = False
    source = "Mock up"
    target = "Accounts"

    'Get last Row of target Sheet and temp value to search.
    lastRow = Sheets(target).Range("A" & Rows.count).End(xlUp).row
    tempVal = Sheets(source).Range("B4")

    'Check the search value against Column A on "Accounts"
    For lRow = 1 To lastRow
        'Copy from MockUp to Accounts
        If Sheets(target).Cells(lRow, "A") = tempVal Then
            Sheets(target).Cells(lRow, "B") = Sheets(source).Range("B10")
            Sheets(target).Cells(lRow, "C") = Sheets(source).Range("B8")
            Sheets(target).Cells(lRow, "D") = Sheets(source).Range("B9")
            Sheets(target).Cells(lRow, "E") = Sheets(source).Range("B19")
            Sheets(target).Cells(lRow, "F") = Sheets(source).Range("B18")
            Sheets(target).Cells(lRow, "G") = Sheets(source).Range("B17")
            Sheets(target).Cells(lRow, "H") = Sheets(source).Range("B16")
            Sheets(target).Cells(lRow, "I") = Sheets(source).Range("B22")
            Sheets(target).Cells(lRow, "J") = Sheets(source).Range("B23")
            Sheets(target).Cells(lRow, "K") = Sheets(source).Range("B24")
            Sheets(target).Cells(lRow, "L") = Sheets(source).Range("B25")
            match = True
        End If
    Next lRow

    'No match found in "A", now searching "E"
    If match = False Then
        For lRow = 1 To lastRow
            'Copy from Accounts to MockUp
            If Sheets(target).Cells(lRow, "E") = tempVal Then
                Sheets(source).Range("B10") = Sheets(target).Cells(lRow, "B")
                Sheets(source).Range("B8") = Sheets(target).Cells(lRow, "C")
                Sheets(source).Range("B9") = Sheets(target).Cells(lRow, "D")
                Sheets(source).Range("B19") = Sheets(target).Cells(lRow, "E")
                Sheets(source).Range("B18") = Sheets(target).Cells(lRow, "F")
                Sheets(source).Range("B17") = Sheets(target).Cells(lRow, "G")
                Sheets(source).Range("B16") = Sheets(target).Cells(lRow, "H")
                Sheets(source).Range("B22") = Sheets(target).Cells(lRow, "I")
                Sheets(source).Range("B23") = Sheets(target).Cells(lRow, "J")
                Sheets(source).Range("B24") = Sheets(target).Cells(lRow, "K")
                Sheets(source).Range("B25") = Sheets(target).Cells(lRow, "L")
            End If
        Next lRow
    End If
End Sub

note: If more than one match exists, the last one will overwrite the first. 注意:如果存在多个匹配项,则最后一个将覆盖第一个。 This will loop through the entire Accounts Sheet. 这将遍历整个帐户表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM