Global variable

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
Hi all, is it possible to declare a variable in ThisWorkbook and use the variable in module procedures? Basically i want to create a global variable somewhere that allows me to use the variable in events, modules and procedures. i'm using excel 2003
 

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,776
... Basically i want to create a global variable somewhere that allows me to use the variable in events, modules and procedures. i'm using excel 2003
Declare a public variable in a general module, not in the workbook module or in a worksheet module.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,965
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
FYI< you can declare it in the workbook (or worksheet) module but then you have to refer to it as a member of that object - ThisWorkbook.Variablename for example rather than just variablename.
 

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
ohhhhhh no wonder. alright thanks for the info rorya~
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,776
Hi Rory

A bit of fanaticism for a nice start of the day. :)

It's true that you can declare a public variable in an object module (sadly, in my opinion). I think, however, that this goes against the object model paradigm, goes agains one of the fundamental characteristics of object oriented design, that is Encapsulation. In my opinion it's not good practice to declare public variables in a class. I go even as far as to say that it should even not be allowed by the syntax.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,965
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
I would agree completely but I thought that getting into Property procedures was probably a bit OTT for this discussion. ;)
As a return bit of dogma, public variables should be avoided if at all possible. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,098,879
Messages
5,465,236
Members
406,419
Latest member
ldp124

This Week's Hot Topics

Top