I am working on a specific project where I need to loop through a set of user form objects and do something to each of them (in the case it’s a label). I have them named lbl1, lbl2, lbl3, … so on and so forth. I need to find a way to loop through and toggle the visibility of them with my loop.
I have tried the following to no avail:
(This is an incomplete portion of the code only using the relevant information)
Dim i as Long
Dim result as Object
'… ‘code to define what i is
Set result = “lbl & i
'… ‘ more code to continue the loop
You can utilize Controls
collection. Give a try-
Private Sub CommandButton1_Click()
Dim i As Long
For i = 1 To 3
Me.Controls("lbl" & i).Visible = False
Next
End Sub
You can also use Set
method. Then it would be like-
Private Sub CommandButton1_Click()
Dim i As Long
Dim lb As Object
For i = 1 To 3
'Me.Controls("lbl" & i).Visible = False
Set lb = Controls("lbl" & i)
lb.Visible = False
Next
End Sub
You can also declare variable as Control
like Dim lb As Control
.