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.
jcherry_99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
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
0
GrahamSkanRetiredCommented:
or
For each ctl in UserForm3.Controls
    if left$(ctl.name,5) = "Label" then
           ctl.BackColor = RGB(240, 100, 0)
   endif
next ctl

Incidentally how does one 'pole' anything, label controls included?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

Jerome
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GrahamSkanRetiredCommented:
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.
Graham
0
GrahamSkanRetiredCommented:
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)
   endif
next ctl
end sub

0
vinnyd79Commented:
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.
0
KarcOriginCommented:
Hi,
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
    Next
End Sub

Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.