View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rowan Drummond[_3_] Rowan Drummond[_3_] is offline
external usenet poster
 
Posts: 414
Default Public variables and scope

Public variables should be declared in a standard module and not a Class
Module. The modules behing forms, sheets and the ThisWorkBook module are
all Class Modules.
Move the declaration to a standard module.

Hope this helps
Rowan

Dennis Benjamin wrote:
Hey All

I'm learning VBA in Excel and am confused about variable scope. I have
declared a variable at the top of the thisWorkBook module as follows:

Option Explicit
Public mAb

when I add mAb to the Watchlist, and select Context = thisWorkbook, I can
see the contents. When I add another copy to the Watchlist, selecting
Context=(All Modules) it shows up as "Expression not defined". I am guessing
that this is the reason I am unable to store values in mAb from inside Subs
in my UserForms - they don't know about the variable. Is there some setting
that would cause this behavior? How can I share a variable between my
thisWorkBook and a UserForm?

Thanks!