Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Static variable

How can I save the value of the variable so, that it keeps its value also
after the application has closed?

I have module, where I have:
Sub counting()
Static counter As Integer

counter = counter +1

End sub

But if I save and close the program, and open it again, the value of the
counter is again 0. Any suggestions to fix the problem?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Static variable

You could save it to registry on closing, and read it from there on opening.
See SaveSetting and GetSetting in VBA help

--
__________________________________
HTH

Bob

"VBA beginner" wrote in message
...
How can I save the value of the variable so, that it keeps its value also
after the application has closed?

I have module, where I have:
Sub counting()
Static counter As Integer

counter = counter +1

End sub

But if I save and close the program, and open it again, the value of the
counter is again 0. Any suggestions to fix the problem?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Static variable

Hi,

You could write it somehere out of the way and then read it in next time the
workbook is opened

Sheets("Sheet1").Range("IV65536").Value = counter

and on opening
counter = Sheets("Sheet1").Range("IV65536").Value

Mike

"VBA beginner" wrote:

How can I save the value of the variable so, that it keeps its value also
after the application has closed?

I have module, where I have:
Sub counting()
Static counter As Integer

counter = counter +1

End sub

But if I save and close the program, and open it again, the value of the
counter is again 0. Any suggestions to fix the problem?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Static variable

If the variable name is numvar then just use a basic text file like this in
a button on the worksheet:

numvar=Range("A1").Value
open "C:\excel\numstore.csv" for output as #1
print #1,numvar
close #1

then to retrieve another button or use the Worksheet.activate event:

open "C:\excel\numstore.csv" for input as #1
read #1,numvar
Range("A1").Value=numvar
close #1

or something

scooper


"VBA beginner" wrote in message
...
How can I save the value of the variable so, that it keeps its value also
after the application has closed?

I have module, where I have:
Sub counting()
Static counter As Integer

counter = counter +1

End sub

But if I save and close the program, and open it again, the value of the
counter is again 0. Any suggestions to fix the problem?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Static variable

First of all, unless you specific need an Integer variabel, you should
define your Counter as Long.


Second, this should do the trick:

Sub SaveCounter()

Dim Counter As Long
Counter = GetSetting("This_Is","My_Counter","So_Far", 0)

Counter = Counter + 1

... some code here ...

END_OF_SUB:

SaveSetting "This_Is","My_Counter","So_Far"

End of Sub


"VBA beginner" skrev i en meddelelse
...
How can I save the value of the variable so, that it keeps its value also
after the application has closed?

I have module, where I have:
Sub counting()
Static counter As Integer

counter = counter +1

End sub

But if I save and close the program, and open it again, the value of the
counter is again 0. Any suggestions to fix the problem?





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
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE CAPTGNVR Excel Discussion (Misc queries) 2 February 1st 07 07:30 PM
Static variable RobcPettit[_2_] Excel Programming 1 January 28th 07 01:27 PM
Static variable cottage6 Excel Programming 0 June 24th 05 07:52 PM
Static Variable Todd Huttenstine Excel Programming 7 May 7th 04 11:16 AM


All times are GMT +1. The time now is 10:26 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"