SOLVED

Declaring a variable public in VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-1768079%22%20slang%3D%22en-US%22%3EDeclaring%20a%20variable%20public%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1768079%22%20slang%3D%22en-US%22%3E%3CP%3EI%20declared%20a%20Public%20variable%20at%20the%20top%20of%20the%20Workbook%20module%20(immediately%20after%20Option%20Explicit)%3C%2FP%3E%3CP%3Eand%20initialized%20it%20in%20Workbook_open.%26nbsp%3B%20I%20added%20a%20Debug.Print%20statement%20to%20Workbook_open%20to%20make%20sure%20the%20variable%20was%20being%20initialized%20when%20I%20opened%20the%20Workbook.%26nbsp%3B%20That%20much%20is%20working%20correctly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20variable%20is%20used%20by%20a%20subroutine%20in%20a%20different%20module%2C%20%22module1%22.%26nbsp%3B%20But%20when%20that%20subroutine%20is%20called%2C%20VBA%20flags%20the%20variable%20as%20unknown.%26nbsp%3B%20The%20only%20way%20I%20was%20able%20to%20get%20it%20to%20work%20was%20by%20putting%20an%20identical%20Public%20declaration%20at%20the%20top%20of%20module1.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENothing%20I%20have%20read%20indicates%20that%20a%20Public%20variable%20must%20be%20declared%20in%20every%20module%20in%20which%20it%20is%20used.%26nbsp%3B%20Is%20that%20true%3F%26nbsp%3B%20Or%20is%20there%20something%20special%20about%20the%20case%20I%20described%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1768079%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1768100%22%20slang%3D%22de-DE%22%3ESubject%3A%20Declaring%20a%20variable%20public%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1768100%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382386%22%20target%3D%22_blank%22%3E%40perkin_warbeck%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20your%20permission%2C%20if%20I%20can%20recommend.%20It%20can%20help%20us%20all%20if%20you%20upload%20an%20Excel%20file%20(without%20sensitive%20data)%2C%20no%20picture.%3C%2FP%3E%3CP%3EEven%20if%20it%20is%20said%20that%20a%20picture%20can%20say%20a%20thousand%20words%2C%20it%20is%20certainly%20not%20in%20the%20case%20of%20Excel%2C%20on%20the%20contrary%20in%20some%20cases.%3C%2FP%3E%3CP%3EYou%20could%20get%20a%20precise%20solution%20much%20faster%20with%20a%20file%20(w%2Fout%20sensitive%20data).%3C%2FP%3E%3CP%3EThis%20would%20also%20be%20a%20blessing%20for%20all%20of%20us%2C%20as%20we%20can%20understand%20the%20problem%20much%20better%2C%20a%20win-win%20situation%20for%20everyone.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22%22%3EIt%20is%20also%20helpful%20to%20know%20the%20operating%20system%20and%20Excel%20version%2C%20as%20different%20approaches%20may%20be%20required%20depending%20on%20the%20version%20and%20OS.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20understanding%20and%20patience%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1768101%22%20slang%3D%22en-US%22%3ERe%3A%20Declaring%20a%20variable%20public%20in%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1768101%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382386%22%20target%3D%22_blank%22%3E%40perkin_warbeck%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20declare%20a%20public%20variable%2C%20do%20so%20in%20a%20standard%20module%20(the%20kind%20that%20you%20create%20by%20selecting%20Insert%20%26gt%3B%20Module)%20instead%20of%20in%20the%20ThisWorkbook%20module.%20You%20only%20need%20to%20do%20this%20in%20one%20module.%3C%2FP%3E%0A%3CP%3EMake%20sure%20that%20you%20use%20the%20keyword%20Public%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPublic%20variablename%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20still%20initialize%20it%20in%20Workbook_Open%20in%20the%20ThisWorkbook%20module.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E(You%20can%2C%20in%20fact%2C%20declare%20it%20in%20ThisWorkbook%2C%20but%20then%20you%20%3CEM%3Emust%3C%2FEM%3E%20refer%20to%20it%20as%20ThisWorkbook.variablename%20instead%20of%20just%20variablename)%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I declared a Public variable at the top of the Workbook module (immediately after Option Explicit)

and initialized it in Workbook_open.  I added a Debug.Print statement to Workbook_open to make sure the variable was being initialized when I opened the Workbook.  That much is working correctly.

 

The variable is used by a subroutine in a different module, "module1".  But when that subroutine is called, VBA flags the variable as unknown.  The only way I was able to get it to work was by putting an identical Public declaration at the top of module1.

 

Nothing I have read indicates that a Public variable must be declared in every module in which it is used.  Is that true?  Or is there something special about the case I described?

3 Replies
Highlighted

@perkin_warbeck 

 

With your permission, if I can recommend. It can help us all if you upload an Excel file (without sensitive data), no picture.

Even if it is said that a picture can say a thousand words, it is certainly not in the case of Excel, on the contrary in some cases.

You could get a precise solution much faster with a file (w/out sensitive data).

This would also be a blessing for all of us, as we can understand the problem much better, a win-win situation for everyone.

It is also helpful to know the operating system and Excel version, as different approaches may be required depending on the version and OS.

 

Thank you for your understanding and patience

 

Nikolino

I know I don't know anything (Socrates)

Highlighted
Best Response confirmed by perkin_warbeck (Occasional Contributor)
Solution

@perkin_warbeck 

To declare a public variable, do so in a standard module (the kind that you create by selecting Insert > Module) instead of in the ThisWorkbook module. You only need to do this in one module.

Make sure that you use the keyword Public:

 

Public variablename

 

You can still initialize it in Workbook_Open in the ThisWorkbook module.

 

(You can, in fact, declare it in ThisWorkbook, but then you must refer to it as ThisWorkbook.variablename instead of just variablename)

Highlighted

@Hans Vogelaar Thank you.  Your solution worked perfectly.