Vlookup macro doesn't work

Potatokat

New Member
Joined
May 17, 2022
Messages
5
Office Version
  1. 365
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.

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:
1654018541493.png
 
Last edited by a moderator:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
When posting code use the Quick wrap selection as VBA code.

It will avoid the problem that you have encountered.

1654019335051.png
 
Upvote 0
Good to hear you got the solution.

If you would like to post the solution then it is perfectly fine to mark your post as the solution to help future readers. Otherwise, please do not mark a post that doesn't contain a solution.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top