VBA macro to activate open workbook based on cell value from another open workbook

sirbudan82

New Member
Joined
Feb 3, 2016
Messages
16
I'm stuck with a simple code that I can't make it work. I have two open workbooks and I would like to activate the other workbook from the active workbook based on a cell value. The idea is to activate and close the workbook that includes the database keep the one with the output.

I'm using the code below but I'm getting an error "Subscript out of range"

Sub Switch_Workbooks()
Workbooks(ActiveSheet.Range("B20").Value).Activate
End Sub

Another code line that I could use is Windows("Name Workbook").Activate but I can't make it work to get the value of a cell

Thank you a lot
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Do you have the fill file name in B20 including the file extension? It should work then. B20=Book2.xlsx works, B20=Book2 = "subscript out of range" error.
 
Upvote 0
@Scott T
FYI. If you have have your system settings set to Hide extensions for known file types both of your examples will work.
 
Upvote 0
@Scott T
FYI. If you have have your system settings set to Hide extensions for known file types both of your examples will work.

Interesting.


To the OP make sure the file name is spelled correctly and there are no extra spaces.
 
Upvote 0
@Scott T @Fluff
Thank you for the help but it is still not working. You can see below the content of the cell B20
"C:\Finance Folder\2017\10 - October\Management Reports\MMR_DB @'17 FX Rates.xlsb"
The easiest way would be to be able to use Windows("Name Workbook").Activate but to get the name of the workbook from a cell.
 
Upvote 0
If both workbooks are open, then you don't need the filepath, just the name. Try changing the value in B20 to MMR_DB @'17 FX Rates.xlsb
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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