Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Declaring variables

How can I set a variable to be both Public and Static? I need it to work in
two separate procedures, and I also need it to keep a running tab each time
the macro is run.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Declaring variables

Public variables are static in that they retain their values even if a
particular procedure has stopped running. To be Public the variable has to
be available to all modules at all times, so they exist and retain their
values as long as your project is running (which, for Excel, means as long as
your workbook containing the code is open). So nothing special should be
needed to do what you want to do.
--
- K Dales


"Mike" wrote:

How can I set a variable to be both Public and Static? I need it to work in
two separate procedures, and I also need it to keep a running tab each time
the macro is run.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Declaring variables

Public variables declared at the regular code module level retain their
value until you do something to reset them, so there's no need to
declare them Static.

Try:

Public nTest As Long

Public Sub Test1()
MsgBox nTest
nTest = nTest + 1
End Sub

Public Sub Test2()
MsgBox nTest
nTest = nTest + 2
End Sub

Run Test1 and Test2 a few times...

Note that Public variables declared at the Class Module level retain
their values only as long as an instance of the class exists.

In article ,
"Mike" wrote:

How can I set a variable to be both Public and Static? I need it to work in
two separate procedures, and I also need it to keep a running tab each time
the macro is run.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Declaring variables

This is what I needed. I didn't know the Subs themselves had to have Public
in front of them. That worked. Thanks.

"JE McGimpsey" wrote:

Public variables declared at the regular code module level retain their
value until you do something to reset them, so there's no need to
declare them Static.

Try:

Public nTest As Long

Public Sub Test1()
MsgBox nTest
nTest = nTest + 1
End Sub

Public Sub Test2()
MsgBox nTest
nTest = nTest + 2
End Sub

Run Test1 and Test2 a few times...

Note that Public variables declared at the Class Module level retain
their values only as long as an instance of the class exists.

In article ,
"Mike" wrote:

How can I set a variable to be both Public and Static? I need it to work in
two separate procedures, and I also need it to keep a running tab each time
the macro is run.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Declaring variables

They don't - it's implied if you omit them.

In article ,
"Mike" wrote:

I didn't know the Subs themselves had to have Public
in front of them.

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
Declaring variables freekrill Excel Discussion (Misc queries) 2 July 19th 06 06:36 AM
Declaring variables freekrill Excel Discussion (Misc queries) 0 July 18th 06 06:15 PM
DEclaring variables Pedro Excel Programming 1 November 13th 03 05:02 PM
Declaring variables Pedro Excel Programming 1 November 13th 03 03:32 PM
DEclaring variables Pedro Excel Programming 2 November 13th 03 11:54 AM


All times are GMT +1. The time now is 09:37 AM.

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"