多个工作簿上的Vlookup - VBA

时间:2014-03-03 18:12:53

标签: vba excel-vba vlookup excel

我有以下方法:

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

但它会引发错误。

你能帮我吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

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