#1
|
|||
|
|||
Declaring a variable that is known across modules
Hi,
I have a number of variables declared in one module which I need in a second module which is called from the first one. The variables are all declared using the keyword "Public", but it seems those are global only within one module. How can I either declare them cross-module-global or just transfer them from one module to the next? Thanks a lot! Best regards, Officer_Bierschnitt P.S.: I think I have understood what I've found online insofar that I can create another module, call it "Declarations" and there declare all my variables as PUBLIC and they will be global across modules. Right? Do I then have to call my first module from that one? |
#2
|
|||
|
|||
You don't need a separate module, but it's a good idea. Just declaring them as Public is sufficient to make them visible throughout the project.
You just can't have another variable more local in scope with the same name. |
#3
|
|||
|
|||
Quote:
Thanks a lot! Best regards, DataNibbler P.S.: How would I have to do this with a separate module? I haven't found a specific How-to for this. Does there have to be a Sub() in there with the CALL-command for the next module? |
#4
|
||||
|
||||
If you declare a Public variable in a class module (for example a worksheet or ThisWorkbook module) then the variable may only be accessed as a member of that class. Therefore you must refer to it using syntax such as Sheet1.Variablename or ThisWorkbook.Variablename. If the variable is declared in a normal module, you may simply refer to it using its name from any other module in the workbook.
|
#5
|
|||
|
|||
Quote:
I have already tried that. It seems to work. Let's see. Best regards, Officer_Bierschnitt |
#6
|
|||
|
|||
The reason for declaring them is a separate module is just hygiene. They are not specific to any other module (that's why they're public), and keeping them together lets you find them all at a glance. For someone trying to understand your code, it's a good place to start reading.
Public variables have their place, but should be used only after careful consideration of the alternatives. In general, they can make code harder to debug and maintain. The only code I might include in that module is a routine that initializes those variables. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Run Time Error '91': Object variable or With block variable not set using Catalogue Mailmerge | Berryblue | Mail Merge | 1 | 11-13-2014 05:36 PM |
Modelling training modules | Vincent | Project | 3 | 09-03-2014 05:37 PM |
Run-time error 91 object variable or with block variable not set | JUST ME | Word VBA | 4 | 03-25-2014 06:56 AM |
Empty Modules | Greg S. | Excel Programming | 2 | 07-30-2013 01:38 PM |
Run-time error '91': Object variable or With block variable not set | tinfanide | Excel Programming | 2 | 06-10-2012 10:17 AM |