How to Use a VBA For Loop with UserForm Text Boxes in Excel
Question details
The user needs to populate 100 sequentially named text boxes (txt1 to txt100) in a UserForm using names stored in an Excel worksheet column.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Developing an Excel VBA UserForm and needing to dynamically assign values to multiple text boxes based on their sequential names.
- Observed behavior
- Attempting to dynamically reference text boxes using variable concatenation like txt(i) results in a VBA syntax error because the variable is not recognized as part of the object name.
Verify that all 100 text boxes on your UserForm are named sequentially without spaces (e.g., txt1, txt2, txt3) and that your worksheet data is correctly listed in the target column.
Use the UserForm Controls Collection
Use the Me.Controls collection to dynamically construct the text box names as a string within your For Loop.
In VBA, you cannot append a variable directly to an object name (e.g., txt(i+1)). Instead, you must use the UserForm's Controls collection, which allows you to call any control by passing its name as a concatenated string.
Press Alt + F11 to open the Visual Basic for Applications Editor, then double-click your UserForm in the Project Explorer.
Right-click the UserForm and select 'View Code'. From the top-right dropdown menu, select the 'Initialize' event.
Declare your loop counter by typing 'Dim i As Integer' and start the loop with 'For i = 1 To 100'.
Inside the loop, assign the worksheet cell values to the text boxes using the Controls collection: Me.Controls("txt" & i).Text = Cells(i, 2).Value.
End the loop with 'Next i' and run the UserForm to verify that txt1 through txt100 are populated with data from column B.

Create Advanced Macros and UserForms with WPS Office
WPS Spreadsheet provides robust support for VBA macros, allowing you to design powerful UserForms, automate repetitive data entry, and streamline your workflow effortlessly.
- 1. Enable Developer Tools: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled.
- 2. Open the VBA Editor: Navigate to the Developer tab and click on 'Visual Basic' (or press Alt + F11).
- 3. Design Your UserForm: Insert a new UserForm and add your sequentially named text boxes from the toolbox.
- 4. Run the Macro: Paste your Me.Controls For Loop code into the UserForm module and execute the macro.

Frequently Asked Questions
Why do I get a 'Could not find the specified object' error?
This error occurs if the loop generates a control name that does not exist on your UserForm. Verify that your text boxes are named correctly without missing numbers in the sequence (e.g., txt1 to txt100).
Can I use this method to read values from text boxes and write them to a worksheet?
Yes. You can reverse the assignment in your loop by writing Cells(i, 2).Value = Me.Controls("txt" & i).Text inside a CommandButton click event to export the UserForm data.
Does the Controls collection work for CheckBoxes or OptionButtons?
Yes, the Controls collection works for any UserForm control. Just change the string prefix in the loop to match your control names, for example: Me.Controls("chk" & i).Value = True.




