Excel VBA -在索引匹配公式中使用工作簿和工作表变量名称时出错

neskvpey  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(154)

我想在索引匹配公式中使用工作簿和工作表变量名。
索引匹配公式是在第一个工作簿的B列中查找匹配列值,并将匹配列值返回到第二个工作簿的G列,以Excel公式编写:

=INDEX(SecondWorkbook.NamedWorksheet2.Range("B3:B" & NamedWorksheet2_LASTROW), MATCH(FirstWorkbook.NamedWorksheet2.Range("F3:F" & NamedWorksheet1_LASTROW), SecondWorkbook.NamedWorksheet2("A3:A" & NamedWorksheet2_LASTROW), 0))

然而,当引用第一和第二工作簿作为索引匹配公式中的变量名时,我收到了一个错误。
下面是我的代码:

Sub TEST()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim FirstWorkbookPath
FirstWorkbookPath = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")

Dim SecondWorkbookPath
SecondWorkbookPath = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")

Dim FirstWorkbook As Workbook
Set FirstWorkbook = Workbooks.Open(FirstWorkbookPath)

Dim SecondWorkbook As Workbook
Set SecondWorkbook = Workbooks.Open(SecondWorkbookPath)

Dim FirstWorkbook_LASTROW_X, FirstWorkbook_LASTCOL_X As Long
FirstWorkbook_LASTROW_X = FirstWorkbook.Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
FirstWorkbook_LASTCOL_X = FirstWorkbook.Sheets(2).Range("A1").CurrentRegion.Columns.Count

Dim SecondWorkbookNamedWorksheet As Worksheet
Set SecondWorkbookNamedWorksheet = SecondWorkbook.Sheets(5)

Dim SecondWorkbook_LASTROW, SecondWorkbook_LASTCOL As Long
SecondWorkbook_LASTROW = SecondWorkbook.Sheets(5).Range("A" & Rows.Count).End(xlUp).Row
SecondWorkbook_LASTCOL = SecondWorkbook.Sheets(5).Range("A1").CurrentRegion.Columns.Count

With Application
    .Calculation = xlCalculationManual
    .EnableAutoComplete = False 
End With

FirstWorkbook.Sheets(3).Range("G3:G" & FirstWorkbook_LASTROW_X).Formula = _
"=INDEX('[" & SecondWorkbook & "]" & SecondWorkbookNamedWorksheet & "'!R2C2:R" & SecondWorkbook_LASTROW & "C2,MATCH(RC[-1],'[" & SecondWorkbook & "]" & SecondWorkbookNamedWorksheet & "'!R2C1:R" & SecondWorkbook_LASTROW & "C1,0))"

With Application
    .Calculation = xlCalculationManual
    .EnableAutoComplete = False 
End With

FirstWorkbook.Close SaveChanges:=False
SecondWorkbook.Close SaveChanges:=False

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

错误是由于在VBA公式中引用了工作簿和工作表变量名。
有人知道如何解决这个问题吗?
非常感谢!

yyyllmsg

yyyllmsg1#

范围。公式属性可以用A1表示法设置。看起来你使用的是R1C1符号。尝试设置范围。公式R1C1性质。
https://learn.microsoft.com/en-us/office/vba/api/excel.range.formula
https://learn.microsoft.com/en-us/office/vba/api/excel.range.formular1c1

iih3973s

iih3973s2#

至少,您需要使用对工作簿和工作表名称的(字符串)引用,而不是对对象本身的引用。
为此,请使用工作簿和工作表的Name属性。
更改此:
FirstWorkbook.Sheets(3).Range("G3:G" & FirstWorkbook_LASTROW_X).Formula = _ "=INDEX('[" & SecondWorkbook & "]" & SecondWorkbookNamedWorksheet & "'!R2C2:R" & SecondWorkbook_LASTROW & "C2,MATCH(RC[-1],'[" & SecondWorkbook & "]" & SecondWorkbookNamedWorksheet & "'!R2C1:R" & SecondWorkbook_LASTROW & "C1,0))"
对此:
FirstWorkbook.Sheets(3).Range("G3:G" & FirstWorkbook_LASTROW_X).Formula = _ "=INDEX('[" & SecondWorkbook.Name & "]" & SecondWorkbookNamedWorksheet.Name & "'!R2C2:R" & SecondWorkbook_LASTROW & "C2,MATCH(RC[-1],'[" & SecondWorkbook.Name & "]" & SecondWorkbookNamedWorksheet.Name & "'!R2C1:R" & SecondWorkbook_LASTROW & "C1,0))"
您可能会发现还需要将LASTROW引用 Package 在CStr()中,以便将数字正确转换为字符串,而不使用前导空格。
再一次,这是你需要做的最低限度。

相关问题