Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建使用vlookup变量的Excel VBA_Excel_Vba_Vlookup - Fatal编程技术网

如何创建使用vlookup变量的Excel VBA

如何创建使用vlookup变量的Excel VBA,excel,vba,vlookup,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

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

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


出现语法错误-更改

& myRangeName & ",1,False)


出现语法错误-更改

& myRangeName & ",1,False)


我不知道你为什么要这样设置VLookup生产线:

LookUp = Application.WorksheetFunction.VLOOKUP(Cells(i, 2),"[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",1,False)
    '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)
您已经完成了定义
范围的95%的工作,您可以像这样使用VLookup行(和前一行):

LookUp = Application.WorksheetFunction.VLOOKUP(Cells(i, 2),"[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",1,False)
    '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:添加代码以支持采购订单的附加请求

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

我不知道你为什么要这样设置VLookup生产线:

LookUp = Application.WorksheetFunction.VLOOKUP(Cells(i, 2),"[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",1,False)
    '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)
您已经完成了定义
范围的95%的工作,您可以像这样使用VLookup行(和前一行):

LookUp = Application.WorksheetFunction.VLOOKUP(Cells(i, 2),"[" & myFileName & "]" & mySheetName & "!" & myRangeName & ",1,False)
    '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:添加代码以支持采购订单的附加请求

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


Vlookup是错误发生的地方。请至少告诉我们哪个错误--如果你硬编码Vlookup呢?因此,与其使用变量来构建字符串,不如对其进行硬编码,看看这是否能使其工作。有时一个变量并不是你想象的那样。@Ulli我想他指的是从底部开始的第五行,从
LookUp=
@UlliSchmid开始,上面写着编译错误“预期:列表分隔符”或“Vlookup是错误发生的地方。请至少告诉我们哪个错误-。-如果你硬编码Vlookup怎么办?因此,与其使用变量来构建字符串,不如对其进行硬编码,看看这是否能使其工作。有时变量与您想象的不同。@Ulli我想他指的是从底部开始的第五行,从
LookUp=
@UlliSchmid开始,它表示编译错误“应为:列表分隔符或)”,感谢您清除了我的语法错误,但它不会将任何内容返回到所需的开放单元格中的原始工作簿。我上传了包含宏的工作簿的两个图像(图1)和一个要从中提取信息的图像(图2)。当我在运行后检查代码时,它显示LookUp高亮显示为黄色,并且不包含任何内容。此外,我还对代码的排列进行了一些更新,并添加了一些尝试修复错误的内容。@Alex在最初的帖子中,您要求解决在Vlookup中遇到的错误,现在您要求调试代码。你的代码的目的是什么?在哪列中搜索什么值?将原始工作簿B列中的项目编号与第二个工作簿A列中的项目编号匹配,并将其返回到原始工作簿中相应的单元格。@Alex请帮助我更好地了解您的需要。B列中的项目编号是否来自上图?该工作簿和工作表的名称是什么。下图中是A列吗?工作簿和工作表的名称是什么?你想要什么结果?哪一页和哪一列?B列中的项目编号来自上图。工作簿为“最终范围”,工作表为“表1”。A列来自下图,工作簿称为“2016.08.05”,工作表也称为“表1”。我希望它与项目编号相匹配,并在同一行的单独列中返回单位和价格(在下图中),与“表1”的下一个空白列中上图中的相应项目编号相同在第j列和第K列中。谢谢,这清除了我的语法错误,但它不会将任何内容返回到所需的开放单元格中的原始工作簿。我上传了包含宏的工作簿的两个图像(图1)和一个要从中提取信息的图像(图2)。当我在运行后检查代码时,它显示LookUp高亮显示为黄色,并且不包含任何内容。此外,我还对代码的排列进行了一些更新,并添加了一些尝试修复错误的内容。@Alex在最初的帖子中,您要求解决在Vlookup中遇到的错误,现在您要求调试代码。你的代码的目的是什么?在哪列中搜索什么值?将原始工作簿B列中的项目编号与第二个工作簿A列中的项目编号匹配,并将其返回到原始工作簿中相应的单元格。@Alex请帮助我更好地了解您的需要。B列中的项目编号是否来自上图?该工作簿和工作表的名称是什么。下图中是A列吗?工作簿和工作表的名称是什么?你想要什么结果?哪一页和哪一列?B列中的项目编号来自上图。工作簿为“最终范围”,工作表为“表1”。A列来自下图,工作簿称为“2016.08.05”,工作表也称为“表1”。我希望它与商品编号匹配,并在同一行的单独列中返回单位和价格(在下图中),与上图中j列和K列中“表1”下一个空白列中的对应商品编号相同。感谢这一帮助。感谢这一帮助。