Looping through text boxes on a user form

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
71,945
Office Version
  1. 365
Platform
  1. Windows
On a user form, I have 30 text boxes named txtFund1 through txtFund30. In VBA, I am trying to access these boxes to initialize them, and update values on my Excel spreadsheet with these values.

I can to do all this pretty easily by writing a line for each inidividual text box. However, I want to be more efficient and use a loop. However, I can not get it to work. Here is my code to initialize the values, that isn't working:

Code:
    Dim i As Integer
    Dim MyTextBox
    For i = 1 To 30
        MyTextBox = txtFund & i
        MyTextBox.Value = ""
    Next i

What do I need to do to be able to loop through all 30 text boxes? Keep in mind that there are also other text boxes on my form, so I can't just through all text boxes on the form.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Andrew,

Thank you!!! That was just what I needed.
 
Upvote 0
Try:

Code:
Dim i As Integer 
For i = 1 To 30 
    Controls("txtFund" & i).Value = "" 
Next i


I am wondering about a step that would be before this. I am creating my textboxes in a loop but believe I am not naming them properly because the code above does not work to fill them however if I put in text boxes by hand it does.

Here is what I have tried for creating the textboxes

Sub Add_Stages()
Dim txtB2 As Control
Dim txtB1 As Control
Dim lbl1 As Control
Dim lbl2 As Control
Dim i As Integer
i = 1

For i = 1 To fsrform.Stages.Value

Set lbl1 = fsrform.MultiPage1.Pages("StagesTab").Controls.Add("Forms.Label.1")

With lbl1
.Name = "Stagelbl" & i
.Caption = "Stage " & i & " name:"
.Height = 16
.Width = 80
.Left = 20
.Top = 30 + (i * 30)
End With

Set txtB1 = fsrform.MultiPage1.Pages("StagesTab").Controls.Add("Forms.TextBox.1")

With txtB1
.Name = "Stage" & i
.Height = 16
.Width = 220
.Left = 110
.Top = 30 + (i * 30)
End With

Set lbl2 = fsrform.MultiPage1.Pages("StagesTab").Controls.Add("Forms.Label.1")

With lbl2
.Name = "Dayslbl" & i
.Caption = "Days in Stage " & i & ":"
.Height = 16
.Width = 80
.Left = 340
.Top = 30 + (i * 30)
End With

Set txtB2 = fsrform.MultiPage1.Pages("StagesTab").Controls.Add("Forms.TextBox.1")

With txtB2
.Name = "Days" & i
.Height = 16
.Width = 40
.Left = 430
.Top = 30 + (i * 30)
End With

Next

End Sub
 
Upvote 0
How could i select specific text boxes from multiple text boxes in excel userform vba?

Dear Sir,
How could i select specific text boxes from multiple text boxes in excel userform vba?

please help me in this regards.

Regards.
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,293
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top