Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-17-2015, 09:15 AM
Officer_Bierschnitt Officer_Bierschnitt is offline Declaring a variable that is known across modules Windows 7 64bit Declaring a variable that is known across modules Office 2013
Advanced Beginner
Declaring a variable that is known across modules
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default 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?
Reply With Quote
  #2  
Old 11-17-2015, 04:28 PM
shg shg is offline Declaring a variable that is known across modules Windows 7 64bit Declaring a variable that is known across modules Office 2010 32bit
Advanced Beginner
 
Join Date: Oct 2015
Posts: 55
shg is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 11-18-2015, 12:34 AM
Officer_Bierschnitt Officer_Bierschnitt is offline Declaring a variable that is known across modules Windows 7 64bit Declaring a variable that is known across modules Office 2013
Advanced Beginner
Declaring a variable that is known across modules
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Quote:
Originally Posted by shg View Post
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.
I'll try. One project can encompass several modules, can't it? I was getting some error_messages in the 2nd module yesterday which led me to believe that my variables - already declared as PUBLIC in the 1st module - were not recognized there. Might have been something different, I'm still not very advanced.

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?
Reply With Quote
  #4  
Old 11-18-2015, 01:33 AM
Debaser's Avatar
Debaser Debaser is offline Declaring a variable that is known across modules Windows 7 64bit Declaring a variable that is known across modules Office 2010 32bit
Competent Performer
 
Join Date: Oct 2015
Location: UK
Posts: 221
Debaser will become famous soon enough
Default

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.
Reply With Quote
  #5  
Old 11-18-2015, 02:42 AM
Officer_Bierschnitt Officer_Bierschnitt is offline Declaring a variable that is known across modules Windows 7 64bit Declaring a variable that is known across modules Office 2013
Advanced Beginner
Declaring a variable that is known across modules
 
Join Date: Oct 2015
Posts: 79
Officer_Bierschnitt is on a distinguished road
Default

Quote:
Originally Posted by Debaser View Post
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.
Ah. So, if I've understood you correctly, I have to create a new module in the workbook and declare all the variables as PUBLIC in that module and all other modules will recognize them.

I have already tried that. It seems to work. Let's see.

Best regards,

Officer_Bierschnitt
Reply With Quote
  #6  
Old 11-18-2015, 12:28 PM
shg shg is offline Declaring a variable that is known across modules Windows 7 64bit Declaring a variable that is known across modules Office 2010 32bit
Advanced Beginner
 
Join Date: Oct 2015
Posts: 55
shg is on a distinguished road
Default

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.
Reply With Quote
Reply



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
Declaring a variable that is known across modules Modelling training modules Vincent Project 3 09-03-2014 05:37 PM
Declaring a variable that is known across modules Run-time error 91 object variable or with block variable not set JUST ME Word VBA 4 03-25-2014 06:56 AM
Declaring a variable that is known across modules Empty Modules Greg S. Excel Programming 2 07-30-2013 01:38 PM
Declaring a variable that is known across modules Run-time error '91': Object variable or With block variable not set tinfanide Excel Programming 2 06-10-2012 10:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft