如何创建使用vlookup变量的{4}

时间:2016-08-12 20:47:05

标签: excel vba vlookup

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

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

The first image is of the workbook containing the macro

the second is the workbook from which it extracts info from

2 个答案:

答案 0 :(得分:1)

语法错误 - 更改

.h

& myRangeName & ",1,False)

答案 1 :(得分:1)

我不确定您为什么要设置 VLookup 行:

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

您确实有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