changing selected label on userform w/ a variable

  • Thread starter Thread starter mike k
  • Start date Start date
M

mike k

I am trying to change the text of the labels that I have
on a userform and would like to refer to these labels
using a variable so that I can create a loop. ie.

userform_.labelx=____

where x=some integer.

I have tried this but it hangs everytime.

Thanks
 
mike k said:
I am trying to change the text of the labels that I have
on a userform and would like to refer to these labels
using a variable so that I can create a loop. ie. <snip>

Try these ideas:

Private Sub UserForm_Click()
'you can use the Controls collection like this
'(assumes they are named Label1, Label2, etc.)
Dim i
For i = 1 To 2
UserForm1.Controls("Label" & CStr(i)).Caption = "something"
Next i

'or this
i = 0
Dim ctl As Control
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "Label" Then
i = i + 1
ctl.Caption = "Label" & CStr(i)
End If
Next

'or create a sub-collection of labels to use
Dim colLabels As New Collection
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "Label" Then
colLabels.Add ctl, ctl.Caption '(2nd arg must be a unique string)
End If
Next

colLabels("Label1").Caption = "this"
colLabels("Label2").Caption = "that"

End Sub

'---
Bob
 
Back
Top