ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   changing selected label on userform w/ a variable (https://www.excelbanter.com/excel-programming/291823-changing-selected-label-userform-w-variable.html)

mike k

changing selected label on userform w/ a variable
 
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

Bob Kilmer[_2_]

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




All times are GMT +1. The time now is 02:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com