View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Kilmer[_2_] Bob Kilmer[_2_] is offline
external usenet poster
 
Posts: 71
Default changing selected label on userform w/ a variable

"mike k" wrote in message
...
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