如何创build使用vlookupvariables的Excel VBA

[在工作表中search给定的单元格值以查找其他工作簿的相应信息,并将相应的第一个空列中的原始工作簿返回给它]

Sub Macro1() Dim filename As String Dim myFileName As Workbook Dim mySheetName As Worksheet Dim myRangeName As Range 'get workbook path filename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Please select a file") 'set our workbook and open it Set myFileName = Application.Workbooks.Open(filename) 'set our worksheet Set mySheetName = myFileName.Worksheets("Table 1") 'set the range for vlookup all active rows and columns Set myRangeName = mySheetName.Range("A1").CurrentRegion 'return to the original Workbook ThisWorkbook.Activate Dim LookUp As String Dim returnValue As Variant Dim OriginalCell As String Dim UpdatedCell As String Dim FirstRow As String Set Rng = ActiveSheet.Cells lastRow = Rng.Find(what:="*", after:=Rng.Cells(1), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row WhatToFind = Chr(10) 'Finds all the rows with sequnce numbers then deletes everything in the specified cell after the first line break For i = 1 To lastRow FindRow = Range("A:A").Find(what:=i, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Row If FindRow >= 1 Then OriginalCell = Cells(FindRow, "B").Value UpdatedCell = Left(OriginalCell, InStr(OriginalCell, WhatToFind) - 1) Cells(FindRow, "B").Value = UpdatedCell ' Uses the new cleaned up specified cell and searches another workbook, ' which the user selects and the first work sheet within that workbook and returns ' the corresponding info back to the original workbook in a the the next empty column. LookUp = Application.WorksheetFunction.VLookup(Cells(FindRow, 2), myRangeName, 1, False) Cells(i, "I").Value = LookUp End If Next i End Sub 

第一个图像是包含宏的工作簿

第二个是从中提取信息的工作簿

我不知道为什么你有这样的VLookup线设置:

 LookUp = Application.WorksheetFunction.VLOOKUP(Cells(i, 2),"[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",1,False) 

Youreay做了95%的定义Range的工作,你可以使用VLookup线(和上一行)像这样:

  'set the range for vlookup all active rows and columns Set myRangeName = mySheetName.Range("A1").CurrentRegion ' Uses the new cleaned up specified cell and searches another workbook which the user selects and the first worksheet ' within that workbook and returns the corresponding info back to the original workbook in a cell next to the empty column. LookUp = Application.WorksheetFunction.VLookup(Cells(i, 2), myRangeName, 1, False) 

编辑1:添加代码以支持PO的附加请求。

 Sub Macro1() Dim filename As String Dim myFileName As Workbook Dim currentSheet As Worksheet Dim mySheetName As Worksheet Dim myRangeName As Range Dim lastRow As Long Dim i As Long Dim matchRow As Long 'set current worksheet Set currentSheet = ThisWorkbook.Worksheets("Table 1") 'get workbook path filename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Please select a file") 'set our workbook and open it Set myFileName = Application.Workbooks.Open(filename) 'set searched worksheet Set mySheetName = myFileName.Worksheets("Table 1") ' find last row in Column A ("Item No.") lastRow = mySheetName.Cells(mySheetName.Rows.Count, "A").End(xlUp).Row 'set the range for Vlookup all active rows and columns Set myRangeName = mySheetName.Range("A1:A" & lastRow) ' find last row in Column B in This Workbook ("Item No.") lastRow = currentSheet.Cells(currentSheet.Rows.Count, "B").End(xlUp).Row For i = 2 To lastRow With currentSheet If Not IsError(Application.Match(.Cells(i, "B"), myRangeName, 0)) Then matchRow = Application.Match(.Cells(i, "B"), myRangeName, 0) .Cells(i, "J") = mySheetName.Cells(matchRow, "J").Value .Cells(i, "K") = mySheetName.Cells(matchRow, "Q").Value Else ' Item No. record not found ' put #NA in cells, to know it's not found .Cells(i, "J") = CVErr(xlErrNA) .Cells(i, "K") = CVErr(xlErrNA) End If End With Next i End Sub 

有一个语法错误 – 更改

 & myRangeName & ",1,False) 

 & myRangeName ,1,False)