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:
[color blue]
Code:
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
[/color]
The below will Disable and Lock all of the TextBoxes in the UserForm.
[color blue]
Code:
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
[/color]
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.
[color blue]
Code:
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
[/color]
There are plenty of other variations that can be done doing this. Experiment and have fun!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.