Solved

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

Posted on 2004-10-24
187 Views
Last Modified: 2010-05-02
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.
0
Question by:jcherry_99
    7 Comments
     
    LVL 28

    Assisted Solution

    by:vinnyd79
    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
     
    LVL 76

    Accepted Solution

    by:
    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
     

    Author Comment

    by:jcherry_99
    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
     
    LVL 76

    Expert Comment

    by:GrahamSkan
    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
     
    LVL 76

    Expert Comment

    by:GrahamSkan
    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
     
    LVL 28

    Expert Comment

    by:vinnyd79
    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
     
    LVL 5

    Expert Comment

    by:KarcOrigin
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    Problem to select 11 59
    macro for email automation 16 97
    VBA to format 3 57
    Use closed file on desktop in vba 6 42
    Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now