[VBA] Vlookup formula with Dynamic Workbook

zaeta25

New Member
Joined
Jun 14, 2018
Messages
1
Hello,

I have a problem setting a Vlookup formula with VBA.
When I run the Macro a fileDialog appears asking me for the location of the file several times but that happens just when I use (File as the workbook)

This is my formula

Public File As a Workbook

Dim NBF As Range

Set File = ActiveWorkbook

Workbooks(Template).Worksheets("Data ").Range(NBF.Address).Formula = "=VLOOKUP(C2,'[File]AP'!$H:$L,5,FALSE)"

---------
Now When I use the following code with the name of the file I don't have problems

Workbooks(Template).Worksheets("Data ").Range("K2").Formula = "=VLOOKUP(C2,'[100-583 ACC ES.xlsx]AP'!$H:$L,5,FALSE)"

I will have Dynamic workbook so I don't know how to configure the formula and prevent the Pop up asking for the file location.

Thanks for your help, suggestions or examples.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
try this ( untested)
Code:
nam = ActiveWorkbook.Name

Workbooks(Template).Worksheets("Data ").Range(NBF.Address).Formula = "=VLOOKUP(C2,'[" & nam & "]AP'!$H:$L,5,FALSE)"
 
Upvote 0
try this ( untested)
Code:
nam = ActiveWorkbook.Name

Workbooks(Template).Worksheets("Data ").Range(NBF.Address).Formula = "=VLOOKUP(C2,'[" & nam & "]AP'!$H:$L,5,FALSE)"


This actually worked for me, I kept getting Application Error (1004) but as soon as I set activeworkbook.name, it's all worked out quite nicely. Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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