简体   繁体   English

在多个工作簿上的Vlookup-VBA

[英]Vlookup on multiple Workbooks - VBA

I have the following method: 我有以下方法:

Sub vlookup(ByVal row_index0 As Integer, ByVal row_indexF As Integer, ByVal tgt_row_indexF As Integer, ByVal formula_col_index As Integer, ByVal sheet_name As String)

Dim book1 As Workbook
Dim book2 As Workbook

Set book1 = Workbooks("Summary.xlsm") '<edit as needed
Set book2 = Workbooks("Summary_0.xlsm") '<edit as needed

Cells(row_index0, formula_col_index).Select

ActiveCell.FormulaR1C1 = _
        "=IF(ISNA(VLOOKUP(RC[-2],book2.Sheets(sheet_name).Range(Cells(row_index0,formula_col_index).Address,Cells(tgt_row_indexF,formula_col_index).Address),1,FALSE)),""ERROR"",""OK"")"


End Sub

But it throws an error. 但这会引发错误。

Can you help me please? 你能帮我吗?

Try this one: 试试这个:

Sub vlookup(ByVal row_index0 As Integer, _
            ByVal row_indexF As Integer, _
            ByVal tgt_row_indexF As Integer, _
            ByVal formula_col_index As Integer, _
            ByVal sheet_name As String)

   Dim book1 As Workbook
   Dim book2 As Workbook

   Set book1 = Workbooks("Summary.xlsm") '<edit as needed
   Set book2 = Workbooks("Summary_0.xlsm") '<edit as needed

   Cells(row_index0, formula_col_index).FormulaR1C1 = _
        "=IF(ISNA(VLOOKUP(RC[-2],'[" & book2.Name & "]" & sheet_name & "'!" & _
            Range(Cells(row_index0, formula_col_index), Cells(tgt_row_indexF, formula_col_index)).Address(, , xlR1C1) & _
            ",1,FALSE)),""ERROR"",""OK"")"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM