Cannot share originating workbook name as variable across workbooks

richardpfister

New Member
Joined
Nov 18, 2016
Messages
7
I know how to declare public variables and then reference them from other workbooks but I am unable to make this work when the originating workbook name is the variable. Here is my scenario:

User opens Workbook1 and runs Macro1, which does the following
Code:
Public wb1 As Workbook
Public wb2 As Workbook
Sub Macro1()
Set wb1 = ActiveWorkbook

Workbooks.Open ("Workbook2.xlsm")
Set wb2 = ActiveWorkbook

Application.Run wb2.Name & "!Macro2"
End Sub

Macro2 (in wb2) is called and runs fine until I reference wb1
Code:
Sub Macro2()
wb1.Activate  <code breaks="" here,="" runtime="" error="" 424:="" object="" required="">''it throws a 424 object required error here

It throws an error because wb1 is empty. I want to late-bind it (e.g. Application.Workbooks("Workbook1.xls").variablename) but that requires the very details I'm trying to store in a variable.

Someone's going to ask so here's my end goal: I have 20 workbooks used by multiple users. I want to maintain one master file of macros accessible from the 20. I first tried having the 20 files replace their own VBProjects with those in the master file; that was messy and unsuccessful. So now I'm trying to have the 20 files open the master workbook and run the macros from there. So there's a kick off program to open the master and then run the macros in it. The problem I'm having is that the 20 files must all be named differently, so I don't know how to refer back to the originating file once the master is open and running.

Hopefully this all makes sense and somebody has some ideas. Thanks very much in advance for your time.

- Richard
</code>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
First add a Name in Workbook2 called "CallingBook" and set its value to "" (empty string)

Code:
Sub Macro1()
    Set wb1 = ActiveWorkbook
    Workbooks.Open ThisWorkbook.Path & "/Workbook2.xlsm"
    Set wb2 = ActiveWorkbook
    wb2.Names("CallingBook").RefersTo = ThisWorkbook.Name
    Application.Run wb2.Name & "!Macro2"
End Sub

Sub Macro2()
    Dim CallingBook As String
    CallingBook = Evaluate("CallingBook")
    Windows(CallingBook).Activate
End Sub
 
Upvote 0
Hi Warship,
Thanks for your reply. I got a runtime error 1004 @ line "wb2.Names("CallingBook").RefersTo = ThisWorkbook.Name" when trying to run this. So I tweaked it a little and it worked just fine, thanks!!

(modified line):
Code:
wb2.Names.Add Name:="CallingBook", RefersTo:=ThisWorkbook.Name
 
Upvote 0
First add a Name in Workbook2 called "CallingBook" and set its value to "" (empty string)

Your are welcome.

The 1004 was likely due to the Name not existing in wb2.

I had forgotten that "wb2.Names.Add" simply updates when the Name DOES exist.
So that code should work going forward even after the Name is already there.
Guess I pulled a Homer on that one... DOH!

All the better though. Glad it worked out for you!
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,976
Members
448,934
Latest member
audette89

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