Userform Textbox

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
In my module, I have some textboxes: textbox1, textbox2, textbox3,etc. I want to create a loop to test each of these textboxes and need to refer to them.

for i = 1 to numberofTextbox
if userform1.textbox & i.value = "something" then
' do things
End if
next i

I tried to this but it doesn't work:

for i = 1 to numberofTextbox
if Controls("userform1.Textbox" & i).value = "something" then
..
...


How to do this in a loop so I don't have to explicitly test for each Textbox?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try...

Code:
[font=Verdana][color=darkblue]Private[/color] [color=darkblue]Sub[/color] CommandButton1_Click()
    [color=darkblue]Dim[/color] Ctrl [color=darkblue]As[/color] Control
    [color=darkblue]For[/color] [color=darkblue]Each[/color] Ctrl [color=darkblue]In[/color] Me.Controls
        [color=darkblue]If[/color] TypeName(Ctrl) = "TextBox" [color=darkblue]Then[/color]
            [color=darkblue]If[/color] Ctrl.Value = "Something" [color=darkblue]Then[/color]
                [color=green]'do things[/color]
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] Ctrl
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Code:
    Dim TB As Control, i As Integer, numberofTextbox As Integer
    numberofTextbox = 3
    For i = 1 To numberofTextbox
        Set TB = Me.Controls("TextBox" & i)
        Select Case TB.Value
            Case "Somthing 1"
                'Do somethng here
            Case "Somthing 2"
                'Do somethng to here
            Case "Something 3"
                'Do somethng to here
            Case Else
                'Do somethng to here
        End Select
    Next i
 
Upvote 0
I might have been too general in my posting. The code you provided me reads all the textboxes on the form. But I only want to read specific textboxes on the form. For example, I have textboxes named txtfirstname1, txtfirstname2, txtfirstname3, etc. But I also have other textboxes on the form. I only want to loop thru txtfirstname1, txtfirstname2...txtfirstname10. Is there a way to only loop thru those textboxes?

Reminder, the VBA code is in the Module.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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