VBA & Macro Problems

How to Fix VBA UserForm Save Errors and Subscript Out of Range

Chanuka GeekiyanageChanuka Geekiyanage Nov 11, 2026 869 views

Question details

The user needs to correct a VBA UserForm error where data edits fail to save and a Subscript Out of Range error is triggered due to infinite search loops and improper array indexing.

How to Fix VBA UserForm Save Errors and Subscript Out of Range
Product
Spreadsheets (Excel VBA)
Device & OS
not provided
Scenario
Running a VBA UserForm macro that updates row indexes using the Find/FindNext methods and stores them in an array.
Observed behavior
Edits fail to save, FindNext loops indefinitely because the initial search address is unassigned, and a subscript out of range error occurs at memberRowIndices.
Before you start

Open the VBA IDE (Alt+F11), locate the UserForm code containing the FindNext search loop, and open the Locals Window (View > Locals Window) to monitor the exact values of your array dimensions and search variables.

Solution 1Recommended

Fix the Indefinite FindNext Loop Address Tracking

Assign the first found cell address to a tracking variable before the search loop to prevent FindNext from looping infinitely.

The FindNext method in VBA automatically wraps around to the beginning of the worksheet once it reaches the end. If the address of the first found cell is not explicitly saved and compared against the current search result, the condition evaluates improperly, resulting in an endless loop.

1
Locate the Initial Find Statement

Open your VBA Editor and find the line in your UserForm code where the initial search is performed (e.g., Set f = Range.Find(...)).

2
Store the First Found Address

Immediately after verifying the search found a match (If Not f Is Nothing Then), add the assignment fa = f.Address to store the initial cell location.

3
Update the Loop Termination Condition

Modify the bottom of your search loop to check against the stored address by using Loop While Not f Is Nothing And fa <> f.Address.

Fix the Indefinite FindNext Loop Address Tracking
Variable Declaration Check: Ensure that the variable 'fa' is explicitly declared as a String at the beginning of your procedure to avoid scope and type mismatch errors.
WPS Spreadsheet VBA Editor

Seamlessly Edit and Debug VBA Macros in WPS Office

WPS Spreadsheets provides a fully integrated VBA editor, allowing you to build, troubleshoot, and execute UserForms smoothly. Resolve infinite loops and array indexing errors easily with familiar debugging tools.

  1. 1. Enable Developer Tools: Open your macro-enabled spreadsheet in WPS Office, navigate to the top ribbon, and click on the Developer tab.
  2. 2. Access the VBA Editor: Click the 'Visual Basic' or 'VBA Editor' button to open the integrated IDE where your UserForm scripts are stored.
  3. 3. Apply Code Fixes: Locate the module with your faulty FindNext loop, insert the correct fa = f.Address tracking logic, and run the debugger to verify the fix.
High compatibility with Microsoft Excel (.xlsm) formats and native VBA syntaxIntegrated VBA Editor featuring immediate and locals windows for deep debuggingLightweight footprint for exceptionally fast macro executionCost-effective solution to handle advanced spreadsheet automation workflows
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA FindNext function loop indefinitely?

The FindNext method continuously searches a specified range and wraps back to the start when it reaches the end. If you do not explicitly store the address of the very first cell found and use it as a stopping condition (e.g., Loop While fa <> f.Address), the code will never realize it has checked the entire range.

What causes the 'Subscript out of range' (Error 9) in VBA?

This error happens when you try to access or write to an array index that does not exist. For example, if your array is dimensioned to hold 5 items and your code attempts to access item index 6, VBA will throw a Subscript out of range error. You must use the ReDim statement to ensure the array boundaries are large enough.

How can I dynamically resize an array while preserving its existing data?

Use the ReDim Preserve statement inside your loop. For instance, 'ReDim Preserve myArray(1 To newSize)' expands the upper bound of the array while keeping the values already stored in the lower indexes.

How do I check variable values while a UserForm macro is running?

You can insert a breakpoint by clicking in the left margin next to a line of code in the VBA Editor. When the code execution pauses, hover your mouse over variables like 'memberIndex', or view them actively using the Locals Window (View > Locals Window).