VBA to open workbook using references in current workbook

gberg

Board Regular
Joined
Jul 16, 2014
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I want to have a macro in "Workbook A" that opens a different Workbook. I have two named range references in "Workbook A"

Data_Job_Log_Location = C:\Users\gberg\OneDrive\AJ - DC Operations\Backlogs
Data_Job_Log = Job Log Coins All Jobs- 2022.xlsm

The location and file name will change so I want to be able to reference the "Data_Job_Log_Location" and "Data_Job_Log" ranges in the code to open the file

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
It should end up something like this:
Code:
Data_Job_Log_Location = C:\Users\gberg\OneDrive\AJ - DC Operations\Backlogs
Data_Job_Log = Job Log Coins All Jobs- 2022.xlsm
myFile=Data_Job_Log_Location & "\"& Data_Job_Log
 
Upvote 0
Wouldn't that set the location and file name without being able to change it without going into the code? I probably did a poor job in the explanation.

The workbook I am working in is called "JPR - Monthly Tracker 2022.xlsm" It has a worksheet named "Data". On this worksheet I have the file name and file path references I want the macro to reference. These file names and paths will change so I want to be able to update this info and have the macro still work based on the new values entered

1653829146307.png



This code works to open the file I want
Workbooks.Open Filename:="C:\Users\gberg\OneDrive\AJ - DC Operations\Backlogs\Job Log Coins All Jobs- 2022.xlsm"


I tried using an Indirect reference but it gives an error
Workbooks.Open Filename:=INDIRECT("""&Data_Job_Log_Location&" \ "&Data_Job_Log&""")

Thanks,
 
Upvote 0

Forum statistics

Threads
1,215,147
Messages
6,123,295
Members
449,095
Latest member
Chestertim

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