I'm moving all of the VBA code from an XLSM to a common XLAM that will now be a reference from the XLSM.
The XLSM will have no code, but only worksheets with data and tables.
Because of the extensive use of dynamic named range references in my code [tbData[products]], etc....I had to do an "thisworkbook.Activate" quite often.
Now I can't do that of course....I need the workbook's reference or handle.
Users will want their own "named" workbook.
I can think of two ways to do this:
1) force them to use a standard name for the first part of the WB name....i.e. "XLRMaint....." followed by their own name or special string
2) embed a keyword into the workbook and look for that in Properties when referencing.
To do either of the above, I must iterate all of the open workbooks each time I need a handle on the workbook containing the data.
Of course, these approaches will fail if they open more than one copy of the special workbook....that's another problem that will require an application-level Open event handler.
Another much less desired approach would be to force them to have ONLY the one workbook open....thus, Activeworkbook is always the same.
They are not going to like that however.
Looking for the best approach here...
Workbook reference without depending upon the name
-
- 4StarLounger
- Posts: 584
- Joined: 12 Jul 2012, 10:34
-
- Administrator
- Posts: 78478
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Workbook reference without depending upon the name
I assume that the code will have to operate on the workbook that is active when the code is called, so you could have a variable of type Workbook and set it to ActiveWorkbook at the beginning of the code.
Best wishes,
Hans
Hans
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: Workbook reference without depending upon the name
Can you show us a snippet of code that demonstrates what you are asking about?
-
- 4StarLounger
- Posts: 584
- Joined: 12 Jul 2012, 10:34
Re: Workbook reference without depending upon the name
Essentially I think all I need to do is set a global var or public property to the workbook when it is opened....and then set it to nothing upon close.
Each users Excel instance will need to have a reference set to the addin.
The addin must detect if any workbook being opened is the one with the data sheets....which are uniquely named.
So there's another way of distinguishing the special workbook.....from the actual sheet names.
I'll need to establish a class to trap and handle the open and close events for any workbooks being opened or closed.
Each users Excel instance will need to have a reference set to the addin.
The addin must detect if any workbook being opened is the one with the data sheets....which are uniquely named.
So there's another way of distinguishing the special workbook.....from the actual sheet names.
I'll need to establish a class to trap and handle the open and close events for any workbooks being opened or closed.
-
- Microsoft MVP
- Posts: 656
- Joined: 24 Jan 2010, 17:51
- Status: Microsoft MVP
- Location: Weert, The Netherlands
Re: Workbook reference without depending upon the name
You could add a customDocument property to the workbooks "belonging" to the add-in and check for that.
-
- 4StarLounger
- Posts: 584
- Joined: 12 Jul 2012, 10:34
Re: Workbook reference without depending upon the name
Yep, thanks for that....another way to go.
I've just got to determine which method is most reliable.
I've just got to determine which method is most reliable.