Save Excel VBA UserForm Data Between Sessions
Question details
Keep Excel VBA UserForm values between sessions while allowing the user to discard unsaved edits.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A UserForm contains text fields, an option selection, and notes that should be restored the next time it opens.
- Observed behavior
- Values should be written only when the user selects OK or Update; Cancel should close the form without writing edits.
Open the macro-enabled workbook in Excel and identify the UserForm and control names in the Visual Basic Editor. Make sure the workbook structure is not protected against adding a worksheet, and reserve the name MacroData_Hidden for the storage sheet.
Store UserForm values on a very-hidden worksheet
Use this approach to load saved values when the form opens and write changes only when OK is selected.
The example uses two text boxes named txtName and txtNotes, an option button named optYes, and buttons named cmdOK and cmdCancel. Change the names and cell assignments to match your form. For a group of option buttons, store and restore each selection as needed.
ThisWorkbook refers to the workbook containing the form code. The hidden sheet stores values in that workbook; it is not a security boundary.
Open the Visual Basic Editor, select the UserForm, and open each data control's Properties window. Clear its ControlSource property so edits are not written to worksheet cells before OK is selected.
In the Visual Basic Editor, right-click the UserForm and select View Code. Add these procedures to the form's code module, adapting the control names if needed: Private Sub UserForm_Initialize() Dim ws As Worksheet On Error Resume Next Set ws = ThisWorkbook.Worksheets("MacroData_Hidden") On Error GoTo 0 If ws Is Nothing Then Exit Sub Me.txtName.Value = ws.Range("B1").Value Me.txtNotes.Value = ws.Range("B2").Value If Not IsEmpty(ws.Range("B3").Value) Then Me.optYes.Value = CBool(ws.Range("B3").Value) End If End Sub Private Sub cmdOK_Click() Dim ws As Worksheet On Error Resume Next Set ws = ThisWorkbook.Worksheets("MacroData_Hidden") On Error GoTo 0 If ws Is Nothing Then Set ws = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)) ws.Name = "MacroData_Hidden" End If ws.Visible = xlSheetVeryHidden ws.Range("B1").Value = Me.txtName.Value ws.Range("B2").Value = Me.txtNotes.Value ws.Range("B3").Value = Me.optYes.Value Unload Me End Sub Private Sub cmdCancel_Click() Unload Me End Sub
Run the UserForm, enter values, and select OK. Reopen it to check that the values load; then change a value, select Cancel, and reopen the form again to verify that the earlier value remains.
Select File > Save and keep the workbook in a macro-enabled format such as .xlsm. Save after selecting OK so the worksheet changes persist when Excel is closed.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work. It does not fix Excel VBA UserForm behavior; verify macro and UserForm compatibility before migrating an automated workbook.

Frequently Asked Questions
Why do my UserForm values disappear after I close Excel?
UserForm control values are temporary unless they are stored elsewhere. Write them to a worksheet from the OK button, then use File > Save to save the macro-enabled workbook before closing Excel.
How can Cancel discard changes to the form?
Clear the controls' ControlSource properties and write values to the worksheet only in the OK or Update button event. The Cancel button can then use Unload Me without writing the edits.
What happens the first time the UserForm opens?
If MacroData_Hidden does not exist, the initialization code leaves the controls at their default values. The OK button creates the sheet and stores the entered values.
How do I access the very-hidden storage sheet?
In the Visual Basic Editor, select MacroData_Hidden in the Project Explorer and change its Visible property to xlSheetVisible in the Properties window. Set it back to xlSheetVeryHidden when finished.
Can I save a longer note in one worksheet cell?
Yes, assign the notes text box value to a cell such as MacroData_Hidden!B2. Excel cells have a 32,767-character limit, so longer notes need another storage approach.




