Static variable

V

VBA beginner

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?
 
B

Bob Phillips

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

Mike H

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
 
S

scooper

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to hide password? 1
Static Variable 4
code help 0
sub for calculating distance via post code / variable range. 7
Counter stops working 4
using variable in 7
help with a piece of code 5
Do Until or For Loop 3

Top