Frage

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?

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top