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:
I have tried to save this variable to another cell in the workbook using the following code tied to a button:
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:
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?
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?