Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Adam1 Chicago
 
Posts: n/a
Default Maintaining VBA variable values

How can I get my variables to keep their values when I close my Excel file?

In other words, I have some Dim statements at the top of my code so the
variables keep their values while I run different sub routines; however, when
I close and re-open the file, the values are lost -- is there a way to save
them?

(As you can probably tell, I am a novice so your not missing anything --
this is probably a very easy question.)

Thanks
  #2   Report Post  
Earl Kiosterud
 
Posts: n/a
Default

Adam,

One way is to tuck them into cells in a sheet. And unless you have a
compute-intensive iterative nightmare that runs for a long time, just use
those cells as variables.

Dim MyValue5 As Range
Set MyValue5 = Sheets("Sheet1").Range("A1")
MsgBox MyValue5
MyValue5 = MyValue5 + 1
MsgBox MyValue5

Or use names. Insert - Name - Define. Put the variable name (MyValue5) in
the name box, and =5 (to initially set it to 5) in the "refers to" box.
this doesn't use a cell. Or use this code once:

ActiveWorkbook.Names.Add Name:="MyValue5", RefersToR1C1:="=5"

Now use it like you would a variable
MsgBox MyValue5
MyValue5 = MyValue5 + 1
MsgBox MyValue5

Either way, they get stored with the workbook when saved.
--
Earl Kiosterud
mvpearl omitthisword at verizon period net
-------------------------------------------

"Adam1 Chicago" wrote in message
...
How can I get my variables to keep their values when I close my Excel
file?

In other words, I have some Dim statements at the top of my code so the
variables keep their values while I run different sub routines; however,
when
I close and re-open the file, the values are lost -- is there a way to
save
them?

(As you can probably tell, I am a novice so your not missing anything --
this is probably a very easy question.)

Thanks



  #3   Report Post  
Bob Phillips
 
Posts: n/a
Default

Save them in a range name, and retrieve them later. Make sure the variables
are declared as Public variables in a general module, and use something like

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveWorkbook.Names.Add name:="myVar1", RefersTo:=myVar1
End Sub

Private Sub Workbook_Open()

myVar1 = Evaluate(ActiveWorkbook.Names("myVar1").RefersTo)

End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Adam1 Chicago" wrote in message
...
How can I get my variables to keep their values when I close my Excel

file?

In other words, I have some Dim statements at the top of my code so the
variables keep their values while I run different sub routines; however,

when
I close and re-open the file, the values are lost -- is there a way to

save
them?

(As you can probably tell, I am a novice so your not missing anything --
this is probably a very easy question.)

Thanks



  #4   Report Post  
Fredrik Wahlgren
 
Posts: n/a
Default


"Adam1 Chicago" wrote in message
...
How can I get my variables to keep their values when I close my Excel

file?

In other words, I have some Dim statements at the top of my code so the
variables keep their values while I run different sub routines; however,

when
I close and re-open the file, the values are lost -- is there a way to

save
them?

(As you can probably tell, I am a novice so your not missing anything --
this is probably a very easy question.)

Thanks


You can define a name similar to the name of your variable and refer to the
value of your variable.

/Fredrik



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
Zero values in a log chart Joelle_Smith Charts and Charting in Excel 2 May 8th 23 03:42 AM
Grabbing recods based on date and shift values Jay Excel Worksheet Functions 1 February 25th 05 02:42 PM
How sum values in column B using values in column A as the conditi oldgrayelf Excel Worksheet Functions 5 February 4th 05 09:03 PM
Second serie doesn't use X-as values JackRnl Charts and Charting in Excel 1 January 20th 05 01:04 AM
Variable values in Index function mlkpied Excel Worksheet Functions 6 December 6th 04 11:38 PM


All times are GMT +1. The time now is 11:39 PM.

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

About Us

"It's about Microsoft Excel"