Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Public variables and scope

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!


  #2   Report Post  
Posted to microsoft.public.excel.programming
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!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Public variables and scope

Rowan

Thanks very much for you response ... I'm sure that will solve the problem.
If you're willing to answer one more question, could you explain to me how
to decide whether to place code in an individual sheet, thisWorkBook, or a
Module? I've ordered some books hoping that I will get an overview of the
language, but most resources I've found just jump straight into code w/o
discussing the architecture of an Excel workbook.


Thanks again!

"Rowan Drummond" wrote in message
...
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!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Public variables and scope

Hi Dennis

Generally you would use the Class modules (thisworkbook, and sheet
modules) for events that are associated with that object. The object
being the relevant sheet etc.
A standard module is used for macros which are not associated with a
specific object eg a macro which you can use to format any sheet.

For more info on macros see David McRitchie's intro at
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Chip Pearson has some detail on Events at:
http://www.cpearson.com/excel/events.htm

Hope this helps
Rowan

Dennis Benjamin wrote:
Rowan

Thanks very much for you response ... I'm sure that will solve the problem.
If you're willing to answer one more question, could you explain to me how
to decide whether to place code in an individual sheet, thisWorkBook, or a
Module? I've ordered some books hoping that I will get an overview of the
language, but most resources I've found just jump straight into code w/o
discussing the architecture of an Excel workbook.


Thanks again!

"Rowan Drummond" wrote in message
...

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!




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scope of Variables leerem Excel Discussion (Misc queries) 4 September 30th 08 12:54 PM
Scope of variables Purnima Excel Programming 1 April 5th 05 05:59 AM
Public Variables Les Gordon Excel Programming 2 November 11th 04 12:29 PM
Scope of a public variable Jos Vens Excel Programming 0 November 24th 03 10:08 AM
Scope of Public Variable Dkline[_2_] Excel Programming 9 October 22nd 03 04:53 PM


All times are GMT +1. The time now is 10:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"