• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Changing the color of a label on a form when there are many labels

I am working with a form that has 65 small labels on it. I want to change the color of them to some color that is not in the backcolor pulldown. Putting in 65 lines of code all looking like

userform3.label1.backcolor=rgb(240,100,0) is a night mare

because I want to pole these labels for more important information. Is there a for each command that I can set up.
2 Solutions
Have you considered putting them into a control array. You could then use:

Private Sub Command1_Click()
Dim i As Integer
For i = 0 To UserForm3.Label1.Ubound
    UserForm3.Label1.Item(i).BackColor = RGB(240, 100, 0)
Next i
End Sub
For each ctl in UserForm3.Controls
    if left$(ctl.name,5) = "Label" then
           ctl.BackColor = RGB(240, 100, 0)
next ctl

Incidentally how does one 'pole' anything, label controls included?
jcherry_99Author Commented:
I am perfectly happy with the second answer. I've tried it and it works. But before signing off on the question, I'd like to ask something on the first (I'll increase the number of points).

1. what are you clicking to get to the subroutine you've typed.
2. How do you make the array? I'm not sure I know how to do that.

Thank you both so much for your quick response.

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Hi Jerome
The best place to put the code is somewhere which gets run early in your program run. That is probably in your initial Form_Load event. You won't have to click anything.
Sorry, that means
Sub Form_Load '(in Userform3)
For each ctl in Me.Controls
    if left$(ctl.name,5) = "Label" then
           ctl.BackColor = RGB(240, 100, 0)
next ctl
end sub

To create an array you would add a label and set the index property to 0. Then you would add more labels with the same name and they would each have a different index.They can then be referred to by index #.

Label1(0).Caption = "Label 1 Text"
Label1(1).Caption = "Label 2 Text"
Label1(2).Caption = "Label 3 Text"

The advantage to this is that a VB form can only contain 255 controls.By adding them into a control array,all 65 Labels will count as 1.
Although you are satisfied with GrahamSkan comments here I am with one more way to do that-

Private Sub Form_Load()
    Dim obj As Object
    For Each obj In Me.Controls
        If TypeName(obj) = "Label" Then
            obj.BackColor = RGB(240, 100, 0)
        End If
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now