Variables - simple question

  • Thread starter Thread starter Squid
  • Start date Start date
S

Squid

Can I dim a public variable anywhere in a project or does it have to be in a
module?

I have a variable in my workbook_open event. I now need to use this
variable elsewhere in my project. What is the proper way to do this?
 
Public declarations are made in the declarations section of a module - which
is at the top (see the left dropdown at the top of the module).

In General:
If you want the variable visible to all components of the project, then you
need to declare it in a general module (insert=>Module). Don't declare it
in any of the Class modules like ThisWorkbook, Sheet Modules or Userform
modules.
 
Squid,

Declare the variable in a standard code module, not the
ThisWorkbook module, above and outside of any procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Another approach is to create a public property in the ThisWorkbook
code module and persist its value in a private variable declared the
ThisWorkbook general declarations. You can then refer to it from other
open workbooks e.g.

Application.Workbooks(2).MyProperty

--
 
Back
Top