Saving VBA variables for next time workbook is opened

Special K

Board Regular
Joined
Jun 20, 2011
Messages
83
Office Version
  1. 2010
Platform
  1. Windows
I have VBA variables of types range, long, and string that I would like to save before the workbook is closed so their values will be preserved for the next time the workbook is opened. I have done some searching and so far the only method I can find for doing this is to assign the variables to the value of a cell in the worksheet. Assume I have a variable active_row declared as:

Code:
Dim active_row As Range

I have tried to save this variable to another cell in the workbook using the following code tied to a button:

Code:
Private Sub SaveData_Click()
    Sheets("save").Cells(27, 1).Value = active_row
End Sub

The code executes without error, but there is nothing in the cell afterward. I then save the spreadsheet and close it.

I have another button that is supposed to restore all data saved in this manner:

Code:
Private Sub RestoreData_Click()
    active_row = Sheets("save").Cells(27, 1).Value
End Sub

When I reopen the spreadsheet and click this button, I receive the following error:

Run-time error '91':
Object variable or With block variable not set

I suppose this isn't surprising given that nothing was copied into the cell that was supposed to save my data, but how can I save VBA variables of types range, long, and string before closing the workbook so they can be restored the next time the workbook is opened?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try
Code:
Private Sub SaveData_Click()
    Sheets("save").Cells(27, 1).Value = active_row.Address
End Sub
Private Sub RestoreData_Click()
    active_row = Range(Sheets("save").Cells(27, 1).Value)
End Sub
 
Upvote 0
Try
Code:
Private Sub SaveData_Click()
    Sheets("save").Cells(27, 1).Value = active_row.Address
End Sub
Private Sub RestoreData_Click()
    active_row = Range(Sheets("save").Cells(27, 1).Value)
End Sub

Thanks. The save portion now works (I see an text representation of the range in the indicated location), but when I reopen the workbook and click the RestoreData button, I receive the same error:

Run-time error '91':
Object variable or With block variable not set

Note that active_row is declared at the top of the code file (right after "Option Explicit") as:

Code:
Dim active_row As Range

Is it possible active_row is not being created before the RestoreData button code is executed?
 
Upvote 0
Oops the second one should be
Code:
Private Sub RestoreData_Click()
    [COLOR=#ff0000]Set [/COLOR]active_row = Range(Sheets("save").Cells(27, 1).Value)
End Sub
 
Upvote 0
Oops the second one should be
Code:
Private Sub RestoreData_Click()
    [COLOR=#ff0000]Set [/COLOR]active_row = Range(Sheets("save").Cells(27, 1).Value)
End Sub

Thanks! I just figured that out before I saw your reply.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top