VBA & Macro Problems

How to Use a VBA For Loop with UserForm Text Boxes in Excel

Olivia MillerOlivia Miller Oct 22, 2026 871 views

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.

How to Use a VBA For Loop with UserForm Text Boxes in Excel
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications Editor, then double-click your UserForm in the Project Explorer.

2
Access the Initialize Event

Right-click the UserForm and select 'View Code'. From the top-right dropdown menu, select the 'Initialize' event.

3
Write the For Loop

Declare your loop counter by typing 'Dim i As Integer' and start the loop with 'For i = 1 To 100'.

4
Assign Values via the Controls Collection

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.

5
Close the Loop

End the loop with 'Next i' and run the UserForm to verify that txt1 through txt100 are populated with data from column B.

Use the UserForm Controls Collection
Naming Consistency: Ensure your text boxes in the Properties window are named exactly with the 'txt' prefix followed by the numbers 1 through 100 without any spaces.
Advanced Spreadsheet Capabilities

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. 1. Enable Developer Tools: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled.
  2. 2. Open the VBA Editor: Navigate to the Developer tab and click on 'Visual Basic' (or press Alt + F11).
  3. 3. Design Your UserForm: Insert a new UserForm and add your sequentially named text boxes from the toolbox.
  4. 4. Run the Macro: Paste your Me.Controls For Loop code into the UserForm module and execute the macro.
Fully compatible with Microsoft Excel VBA syntax and UserForm controls.Built-in VBA editor for writing, debugging, and running complex loops.Lightweight installation ensuring fast execution of automated tasks.
QA img-9

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.