dynamically changing a formula for vlookup

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
is there a way to dynamically change the workbook name in a Formula vba statement? I'm looping thru multiple workbooks based on the filename found in the directory and each passthru of the macro would have to reflect the workbook just opened.

first pass workbook name is "Servers with Access.xlsx" Sheet name No Dup
second pass workbook name is "Workstation with Access.xlsx" Sheet name Dups
ect.... until the whole directory is processed

Thanks



Range("J4").Formula = "=IFERROR(VLOOKUP(I4,'[Servers with Access.xlsx]No dup'!$C:$C,1,False),"" "")"
Range("J4").AutoFill Destination:=Range("J4:J" & lastrow)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You can capture the name of the workbook as you open it like this:
Code:
Dim wsName As String
wsName = ActiveWorkbook.Name
and then you can use that in your formula like this:
Code:
[COLOR=#333333]Range("J4").Formula = "=IFERROR(VLOOKUP(I4,'[" & wsName & "]No dup'!$C:$C,1,False),"" "")"[/COLOR]
 
Upvote 0
sweet, thanks so much. I never thought about capturing the workbook name as I opened it.

Worked like a Champ.

Have a Wonderful Day!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,932
Members
452,539
Latest member
delvey

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