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

Create Control Arrays in VBA UserForms by Bowers74
Posted: 28 Aug 03

Some VBA users have read a book or two on Visual Basic and some of them have wished that they could create control arrays in VBA too.

Unfortunately, you can't create control arrays in VBA like in Visual Basic but there is a workaround for certain functions:

The below will insert the two items to each ComboBox in the UserForm.  You can do the same for the TextBoxes like this:

Dim ctrl As Control
For Each ctrl In UserForm1.Controls
    If TypeName(ctrl) = "ComboBox" Then
        With ctrl
            .Clear
            .AddItem "Oil"
            .AddItem "Gas"
        End With
    End If
Next ctrl



The below will Disable and Lock all of the TextBoxes in the UserForm.

Dim ctrl As Control
For Each ctrl In UserForm1.Controls
    If TypeName(ctrl) = "TextBox" Then
        With ctrl
            .Enabled = False
            .Locked = True
        End With
    End If
Next ctrl



If you don't want to "insert" ALL of the controls, you can use the Tag property.

The below will Enable and Unlock all of the TextBoxes in the UserForm that are "tagged" 2.

Dim ctrl As Control
For Each ctrl In UserForm1.Controls
    If TypeName(ctrl) = "TextBox" And ctrl.Tag = "2" Then
        With ctrl
            .Enabled = False
            .Locked = True
        End With
    End If
Next ctrl


There are plenty of other variations that can be done doing this.  Experiment and have fun!

Good Luck!


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