I am trying to write a macro that preforms a vlookup across two open workbooks. I create a column named "risk" in the BJ column of the "exposure" sheet and return my vlookup values from the AK column in my other wookbook (datasource.xlsx) starting in BJ2 and down until there are no more data entries. Nothing happens when I click run with my code below. I'm not sure what I am doing wrong.
I just noticed that my pasted VBA code looks really strange. Here is a screenshot:
VBA Code:
Sub VlookupMultipleWorkbooks()
Dim lookFor As Range
Dim srchRange As Range
Dim book1 As Workbook
Dim book2 As Workbook
Dim book2Name As String
book2Name = "datasource.xlsx"
Set book1 = ThisWorkbook
Set book2 = Workbooks(book2Name)
With Worksheets("exposure")
Dim lastRow As Long
Dim i As Long
Range("BJ1").Formula = "risk"
lastRow = .Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
Set lookFor = book1.Sheets("exposure").Cells(i, "AD") ' value to find
Set srchRange = book2.Sheets(1).Range("$A:$AK") 'source
Range("BJ" & i).Formula = Application.VLookup(lookFor, srchRange, 37, False)
Next i
End With
End Sub
I just noticed that my pasted VBA code looks really strange. Here is a screenshot:
Last edited by a moderator: