Why Word VBA UserForm Controls Lose Values on Re-show After Close
🔍 WiseChecker

Why Word VBA UserForm Controls Lose Values on Re-show After Close

You built a Word VBA UserForm. You fill in text boxes, select list items, and click a command button that hides the form. The next time you show the form, all controls are blank again. This happens because VBA does not persist UserForm control values between instances. Each time the form is closed or unloaded, its memory is cleared. This article explains why control data is lost and how to preserve values across multiple form displays.

Key Takeaways: Preserving UserForm Control Values in Word VBA

  • UserForm Terminate event runs on Close: Unloading the form destroys all control values because the form object is removed from memory
  • Hide method vs Unload statement: Using UserForm.Hide keeps the form in memory and retains values; Unload destroys them
  • Module-level variables as storage: Declare Public variables in a standard module to store control values before the form closes, then repopulate on Initialize

ADVERTISEMENT

Why Word VBA UserForm Controls Reset After Close

A UserForm in Word VBA is an object. When you close the form using the Unload statement or the user clicks the X button, the form object is removed from memory. This triggers the UserForm_Terminate event. All property values of controls on the form — such as TextBox.Text, ComboBox.ListIndex, CheckBox.Value — are lost because the object no longer exists.

The root cause is the difference between Hide and Unload. UserForm1.Hide makes the form invisible but keeps it loaded in memory. All control values remain intact. Unload UserForm1 removes the form from memory, and the next time you call UserForm1.Show, VBA creates a brand new instance of the form. That new instance starts with default values — empty text boxes, unselected list items, and unchecked boxes.

Default Control Values

Each control on a UserForm has a default value set at design time. For a TextBox, the default is an empty string. For a ComboBox, the default ListIndex is -1 (no selection). For a CheckBox, the default Value is False. When a new instance of the UserForm is created, all controls revert to these design-time defaults.

The UserForm_Initialize Event

The Initialize event runs once when the form is first loaded into memory. If you unload and re-show the form, Initialize runs again. Any setup code you placed there — such as populating a ComboBox or setting default text — will execute again. But any runtime values the user entered are gone because the previous instance was destroyed.

Steps to Keep UserForm Control Values When Re-Showing the Form

You have three reliable methods to retain control values. Choose the one that fits your project structure.

Method 1: Use the Hide Method Instead of Unload

  1. Replace Unload with Hide in your command button code
    Instead of Unload Me or Unload UserForm1, write Me.Hide or UserForm1.Hide. This keeps the form in memory.
  2. Show the form again without a new instance
    Call UserForm1.Show again. Because the form is still loaded, VBA does not create a new instance. All control values remain as the user left them.
  3. Handle the QueryClose event to prevent accidental Unload
    Add this code to the UserForm module to intercept the X button:
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
    Cancel = True
    Me.Hide
    End If
    End Sub

Method 2: Store Values in Module-Level Variables

  1. Declare public variables in a standard module
    In a new or existing .bas module, write:
    Public strUserName As String
    Public lngDepartmentIndex As Long
    Public blnNotify As Boolean
  2. Save control values before unloading the form
    In the command button Click event or the UserForm_Terminate event, assign the control values to your public variables:
    strUserName = Me.txtName.Text
    lngDepartmentIndex = Me.cboDepartment.ListIndex
    blnNotify = Me.chkNotify.Value
  3. Restore values in the UserForm_Initialize event
    In the UserForm module, add:
    Private Sub UserForm_Initialize()
    Me.txtName.Text = strUserName
    Me.cboDepartment.ListIndex = lngDepartmentIndex
    Me.chkNotify.Value = blnNotify
    End Sub

Method 3: Use a Custom Class to Wrap the UserForm

  1. Create a class module to hold the form reference
    Insert a class module named CFormManager and add:
    Public WithEvents frm As UserForm1
    Private Sub Class_Initialize()
    Set frm = New UserForm1
    End Sub
  2. Show and hide the form through the class
    In your main code, use:
    Dim mgr As New CFormManager
    mgr.frm.Show
    ' later
    mgr.frm.Hide
    mgr.frm.Show
  3. Let the class persist the form object
    As long as the mgr variable is in scope, the form stays in memory. Control values survive Hide and Show cycles.

ADVERTISEMENT

Common Pitfalls and Edge Cases

UserForm Values Reset When the Document Closes

If the Word document or template that contains the VBA project is closed, all form instances are destroyed. Next time the document opens and the form is shown, a new instance is created with default values. To persist values across document sessions, save the control data to document variables, custom XML parts, or a hidden worksheet in the document.

Multiple Instances of the Same UserForm

If you create multiple instances of the same UserForm using Dim f As New UserForm1, each instance has its own set of control values. Hiding one instance does not affect another. To share values across instances, use public variables or a separate data module.

The Initialize Event Runs Too Early

If you set public variables after the form is already loaded, the Initialize event will not pick them up. Always assign the public variables before calling UserForm1.Show for the first time, or use a custom method like UserForm1.LoadValues that you call after showing the form.

UserForm Controls Lose Values After an Error

An unhandled runtime error can cause VBA to reset the form instance. Wrap your form-showing code in an error handler:
On Error GoTo ErrHandler
UserForm1.Show
Exit Sub
ErrHandler:
Resume Next

Hide vs Unload: Behavior Comparison for Control Value Persistence

Item Hide Method Unload Statement
Form in memory after action Yes No
Control values preserved on re-show Yes No
UserForm_Initialize runs on re-show No Yes
UserForm_Terminate runs on action No Yes
Memory usage over time Higher (form stays loaded) Lower (form is released)
Best use case Single-form, same-session workflow Multi-form, session-end cleanup

Now you can decide whether to keep the form hidden or store values externally. For most single-form scenarios, the Hide method is simplest. For complex projects with multiple forms or cross-session persistence, use module-level variables or custom XML storage. Test each approach with your specific form layout before deploying to users.

ADVERTISEMENT