INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

VBA Visual Basic for Applications (Microsoft) FAQ

VBA How To

Loop Through Controls on a UserForm. Textbox, ComboBox, CheckBox etc by Chance1234
Posted: 12 Jan 06

I am not the original author of the below (unknown), but have pasted this here as it is good information and a question ive seen come up a few times.


Loop Through UserForm Controls

VBA UserForms and their associated controls are a great way to present/collect data from users. There are often occasions however when we need to loop through all controls on a UserForm, or only certain specified controls.

Loop Through All Controls

Use the code below to loop through all Controls on a UserForm

CODE

Private Sub CommandButton1_Click()
Dim cCont As Control
    For Each cCont In Me.Controls
        'DO STUFF HERE
    Next cCont

End Sub
Loop Through Specific/Specified Controls

Use the code below to loop through only specified Controls on a UserForm.

CODE

Private Sub CommandButton1_Click()
Dim cCont As Control

    For Each cCont In Me.Controls
        If TypeName(cCont) = "TextBox" Then
            'DO STUFF HERE
        End If
     Next cCont

End Sub
Loop Through Specific Controls on a Specified Page of a MultiPage Control

Use the code below to loop through only specific Controls on a specified page of a MultiPage Control. Note that Pages(0) is always the first page of any MultiPage Control.

CODE

Private Sub CommandButton1_Click()
Dim cCont As Control

    For Each cCont In Me.MultiPage1.Pages(0).Controls
        If TypeName(cCont) = "TextBox" Then
            'DO STUFF HERE
        End If
     Next cCont

End Sub
Loop Through Specified Controls on all Pages of a MultiPage Control

Use the code below to loop through specified controls on all pages of a MultiPage Control.

CODE

Private Sub CommandButton1_Click()
Dim pPage As Page, cCont As Control

    For Each pPage In Me.MultiPage1.Pages
        For Each cCont In pPage.Controls
            If TypeName(cCont) = "ComboBox" Then
               'DO STUFF HERE
            End If
        Next cCont
    Next pPage

End Sub

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close