VBA vlookup,具有定义的范围和来自其他工作簿的文件

VBA vlookup,具有定义的范围和来自其他工作簿的文件,vba,excel,Vba,Excel,我正在“文件”工作簿中工作。然后,我打开另一个用于查找数据的工作簿(主文件) Workbooks.Open FileName:=Path & Masterfile lRowMasterfile = Cells(Rows.Count, "A").End(xlUp).Row SelectionMasterfile = Range("A1").CurrentRegion.Address Workbooks(File).Activate Range("K2").FormulaR1C1 =

我正在“文件”工作簿中工作。然后,我打开另一个用于查找数据的工作簿(主文件)

Workbooks.Open FileName:=Path & Masterfile

lRowMasterfile = Cells(Rows.Count, "A").End(xlUp).Row
SelectionMasterfile = Range("A1").CurrentRegion.Address

Workbooks(File).Activate

Range("K2").FormulaR1C1 = "=VLOOKUP(RC[-1],'" & Masterfile"' & ! & SelectionMasterfile,1, FALSE)"

Range("K2").AutoFill Destination:=Range("K2:K" & lRowFile)

Workbooks(Masterfile).Close

我定义了主文件和范围,以便在其他文档中使用它,但不幸的是,它不起作用。有人能帮忙吗?

不确定您发布的内容是否有拼写错误,或者您是否直接从代码粘贴了一份副本,但有错误,应该是这样的:

Range("K2").FormulaR1C1 = "=VLOOKUP(RC[-1],'" & Masterfile.Name & "'!" & SelectionMasterfile & ",1, FALSE)"

您处理工作簿就像它们是工作簿中的工作表一样。指定外部工作簿很重要,但从中检索信息的外部工作簿中的工作表也很重要

谢天谢地,通过指定可选的外部参数,可以解决很多问题

Dim mwb As Workbook
Dim lRowMasterfile As Long, lRowFile As Long, SelectionMasterfile As String
Dim Path As String, Masterfile As String, File As String

Path = ThisWorkbook.Path            '<~~ set this properly!
Masterfile = "MasterWorkbook.xlsx"  '<~~ set this properly!
File = ActiveWorkbook.Name  '<~~ set this properly!

Set mwb = Workbooks.Open(Filename:=Path & Masterfile)

With mwb
    With .Worksheets("Sheet1")    '<~~ what worksheet are you trying to get information from?!?
        lRowMasterfile = .Cells(Rows.Count, "A").End(xlUp).Row
        SelectionMasterfile = .Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1, external:=True)
    End With
End With

With Workbooks(File)
    With .Worksheets("Sheet1")    '<~~ what worksheet are you trying to put information into?!?

        lRowFile = 0   '<~~ set this properly!

        'this VLOOKUP only checks to see if the value exists, it doesn't lookup anything but the first column
        'in any event, you can put them all in at the saame time
        .Range("K2:K" & lRowFile).FormulaR1C1 = _
            "=VLOOKUP(RC[-1], " & SelectionMasterfile & ", 1, FALSE)"

    End With
End With

mwb.Close False
Set mwb = Nothing
Dim mwb作为工作簿
将lRowMasterfile设置为长,lRowFile设置为长,选择MasterFile设置为字符串
Dim路径为字符串、主文件为字符串、文件为字符串
Path=thishworkbook.Path'您有两个问题

这一行以A1符号表示地址(例如$A$1:$B$3):

但是您正在使用R1C1表示法(例如R1C1:R3C2)构建公式,并且缺少工作表名称。试试这个:

SelectionMasterfile = ActiveSheet.Name & "!" &  Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)
另一个问题是你的语音标记有错误

Range("K2").FormulaR1C1 = "=VLOOKUP(RC[-1],'[" & Masterfile & "]'" &  SelectionMasterfile & ",1, FALSE)"

PS您应该始终尝试完全限定图纸和范围。查找有关使用
工作簿
工作表
范围
对象变量的指南。

我猜这是您代码的一部分,因为只需在工作表上进行vlookup?如果是,请尝试使用find。此外,请确认您的范围。在
参考样式:=xlR1C1
上有很好的把握。我偷了这个来补充我的回答P@ChipsLetten:thx,我没有考虑R1C1符号。我会查你提到的指南!;-)@Jeeped的答案显示了如何使用对象变量。非常感谢,很抱歉没有发布足够的信息。我把你的代码与=VLOOKUP(RC[-2],“&SelectionMasterfile&”,1,FALSE)”结合在一起,这正好给了我所需要的!记住要给Chipsleten评分。这是他的
参考风格:=xlR1C1
和我的
外部:=True
的组合,似乎就是解决方案。
Range("K2").FormulaR1C1 = "=VLOOKUP(RC[-1],'[" & Masterfile & "]'" &  SelectionMasterfile & ",1, FALSE)"