搜索并填充单元格

时间:2014-12-18 12:27:23

标签: excel vba excel-vba

我需要Excel宏中的搜索功能。

情景:

步骤1)Excel1.xlsx有一列作为主键,我必须在Excel2.xlsx中找到

步骤2)如果在上面的步骤中找到匹配,则必须获取Excel2.xlsx中另一列的值(来自与步骤1匹配的同一行)并填写Excel1.xlsx。

是否有任何VBA宏可以解决此问题?

2 个答案:

答案 0 :(得分:1)

前言如@ L42的评论中所述,VLOOKUP之类的函数是一个更简单的解决方案,具体取决于项目大小,需要是动态的。已经说过了,假设你有一个VBA需求,因为你的问题已被标记,我提供了这段代码。

在不知道您尝试使用的列的情况下,我提供了一个示例场景。我确定你可以通过省略一些变量来简化这一点。我已将它们包含在内,以便只需最少的修改就可以更轻松地设置名称。一旦理解了你想要的概念,就可以随意减少所需的变量。

在示例中,我使用列A作为查找值,列D作为要复制到另一个表的值,如果满足条件。名称和日期是不相关的数据,只是为了给出上下文。 A& D是唯一被扫描或使用的列。

警告:如果您遇到无限循环,以下行可能会导致问题。如果遇到无限循环,则正常的中断模式将不起作用。如果您不希望包含它,那么这不是问题,请注释掉这一行,当第二个工作簿打开时,系统会提示您BREAK。只需点击继续。

仔细查看表格的命名。您需要输入自己的值。

Application.EnableCancelKey = xlDisabled    'Disables breaking when opening new book

<强>试验:

Sub UpdateExternalBook()

Dim s1Sheet As Worksheet, s2Sheet As Worksheet
Dim path As String
Dim s2Name As String, s1SheetName As String, s2SheetName As String
Dim lookupVal As String, moveVal As String
Dim lastS1Row As Long, lastS2Row As Long

'CONFIGURE TO YOUR FILE PATHS HERE
path = "YOUR FILE PATH HERE"                 '"C:\Users\Owner\Documents\"

s2Name = "SECOND BOOK NAME WITH EXTENSION"   '"Book1.xlsm" or "Book1.xlsx"

'Name your Sheets here. 
s1SheetName = "MASTER"                       'SOURCE BOOK SHEET NAME
s2SheetName = "Sheet1"                       'SECOND BOOK SHEET NAME

Application.EnableCancelKey = xlDisabled    'Disables breaking when opening new book

Set s1Sheet = ThisWorkbook.Sheets(s1SheetName)
Set s2Sheet = Workbooks.Open(path & s2Name).Sheets(s2SheetName)

lastS1Row = s1Sheet.Range("A" & Rows.count).End(xlUp).row
lastS2Row = s2Sheet.Range("A" & Rows.count).End(xlUp).row

'BEGIN LOOPING THROUGH ORIGINAL SHEET
    For lRow = 2 To lastS1Row
        lookupVal = s1Sheet.Cells(lRow, "A")     'Lookup Value in Column "A"
        moveVal = s1Sheet.Cells(lRow, "D")       'Value to Copy to second book

        For tRow = 2 To lastS2Row
            If s2Sheet.Cells(tRow, "A") = lookupVal Then
                s2Sheet.Cells(tRow, "D") = moveVal   'Copy data from source to target
            End If
        Next tRow
    Next lRow

'WRAP UP AND CLOSE SECOND WORKBOOK

'Activate and close the second workbook
s2Sheet.Activate
ActiveWorkbook.Close SaveChanges:=True
s1Sheet.Activate

End Sub

原始表格

ORIGINAL

目标之前---&gt; AFTER:

Target Before TARGET AFTER

答案 1 :(得分:0)

是的,您可以,首先在目标DataPreparation下面使用创建数据集,A列中的数据是您的Excel1数据集,B列和C列是您的Excel2数据集,D列包含解决方案。我的解决方案适用于单个Excel文件,如上所述,它使用Excel的vlookup函数。

Sub DataPreparation()
    Range("A1:A5") = Application.WorksheetFunction.Transpose(Array("a", "b", "c", "d", "e"))
    Range("B1:B5") = Application.WorksheetFunction.Transpose(Array("c", "d", "a", "a", "f"))
    Range("C1:C5") = Application.WorksheetFunction.Transpose(Array(3, 4, 1, 1, 6))
End Sub

Sub vLookupMacro()

    On Error Resume Next

    Dim r1 As Range
    Dim r2 As Range
    Dim rOutput As Range

    Set r1 = Range("A1:A5")
    Set r2 = Range("B1:C5")
    Set rOutput = Range("D1:D5")

    For Each x In rOutput
       x.Value = Application.WorksheetFunction.VLookup(r1.Cells(x.Row, 1), r2, 2, 0)
    Next x

End Sub

作为新用户,请参观: https://stackoverflow.com/tour