Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Declaring Variables in Excel VBA so the values stick


Reply
 
Thread Tools Display Modes
  #1  
Old 03-04-2003, 12:42 PM
EACutter EACutter is offline
Newcomer
 
Join Date: Aug 2002
Location: Berkeley, CA
Posts: 24
Default Declaring Variables in Excel VBA so the values stick


I'm encountering some confusion regarding just how to declare my variables to that they will retain their values. Currently I have several modules and I declare most of my variables as Public so they are available throughout. However I find Public variables get erased if I interrupt or debug a macro. I can't use Const and static variables don't seem to work either, that is when I reference a Static variable from another module, I get a compile error.

The Issue is I have several variables that reference specific worksheets (i.e. SanJoaquinCalc = ModelBook.Sheet("San Joaquin Calc") and the are used repeatedly throughout the project, but I would like to run one macro when the spreadsheet is opened to define the sheet name variables and then be done with it. Now I seem to have to run the SheetNames macro at the start of each macro that uses one of the sheet name variables, just to be sure it is defined.
__________________
Eric Cutter
100thMeridian
Energy & Water Resources Consulting
Reply With Quote
  #2  
Old 03-04-2003, 01:53 PM
NateBrei's Avatar
NateBrei NateBrei is offline
Contributor
 
Join Date: Jul 2002
Location: Omaha, NE
Posts: 571
Default

I don't know why you are having problems with the Public variables holding their values when you debug or "break" in your code. They always work fine for me. However, you have mentioned that you have several modules. You will have a problem with Static for sure. Here is how I view variable declarations...

There are 3 levels (scope) of variables depending on where they are declared & what declaration keyword is used to declare them.

Public or Global variables - all procedures in all modules can see & use them. Declared in the General Declarations section of a module using the keyword 'Public'. Even though you can declare public variables in a form module, I would declare all public variables in a standard module (because it is loaded when the application starts & is available throughout the application). Form modules are only available once the form has be loaded & until it is unloaded. So, that could be A source of your problem....

Module level variables - only procedures within that module can see & use them. Procedures in other modules don't know they exist. Declared in the General Declarations section of a module using the keywords 'Dim' or 'Private'. Remain available only as long as the module is loaded (standard - application lifetime; form - form's lifetime).

Local variables - only the procedure in which the variable is declared can see & use them. No other procedure knows they exist. Declared within the procedure (usually immediately following the 'Sub' or 'Function' statement) using the keywords 'Dim' or 'Static'. 'Dim' variables exist only while the procedure is running. Once the procedure exits, the 'Dim' variables are thrown away & recreated on the next entrance into the sub/function (thereby reinitializing their values). 'Static' variables are created the first time a procedure is executed, but unlike 'Dim' variables, they remain even after the procedure exits. When a procedure is re-entered, the 'Static' variable is still there with the value it contained from the last execution of the procedure. It doesn't get deleted until the form is unloaded (form module) or until the application terminates (standard module).

I know this is long-winded, but I've found that a significant number of people are unsure about variables & their declarations & usage. Hope this helps you find your problem. I would start by ensuring all Public variables are in a standard module & not in a form module.

Nate
Reply With Quote
  #3  
Old 03-05-2003, 12:22 AM
icjackson icjackson is offline
Junior Contributor
 
Join Date: Jan 2003
Location: Budapest, Hungary
Posts: 244
Default

Another idea may be to store these variables as custom document properties, and Timbo posted a good site for that here:
http://www.xtremevbtalk.com/show...ferrerid=25947
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
word to excel: cannot copy variables > 1k galenjm Word, PowerPoint, Outlook, and Other Office Products 3 02-04-2003 10:29 PM
find only text values in Excel, not spaces TrentG Word, PowerPoint, Outlook, and Other Office Products 6 11-20-2002 06:13 AM
why does Excel add decimal places to my VB values? petorca Word, PowerPoint, Outlook, and Other Office Products 1 09-08-2002 04:12 AM
How can I check for duplicates before saving a new record GOTzMADsKILLz Database and Reporting 19 08-15-2002 11:56 AM
Retrieving Values out of Excel? bizkit Word, PowerPoint, Outlook, and Other Office Products 2 01-08-2002 10:22 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->