Consulting

Results 1 to 5 of 5

Thread: Formatting multiple textboxes on a UserForm

  1. #1
    VBAX Regular rrtts's Avatar
    Joined
    Sep 2006
    Posts
    61
    Location

    Formatting multiple textboxes on a UserForm

    I have a series of textboxes on a userform. I'd like to color code the textboxes based on their value in order to easily distinguish similar values.

    I have used a code similar to this in the past, but now I have roughy 40 textboxes with 10 different values that can be selected. To use the code below for each value on every textbox would be rather lengthy and very inefficient. However, I can't seem to figure out an alternative solution.

    Any help would be greatly appreciated.

     
    If Userform1.Textbox1.value = "xxx" Then Userform1.Textbox1.BackColor = RGB(xxx, xxx, xxx)

  2. #2
    Quote Originally Posted by rrtts
    I have a series of textboxes on a userform. I'd like to color code the textboxes based on their value in order to easily distinguish similar values.

    I have used a code similar to this in the past, but now I have roughy 40 textboxes with 10 different values that can be selected. To use the code below for each value on every textbox would be rather lengthy and very inefficient. However, I can't seem to figure out an alternative solution.

    Any help would be greatly appreciated.

    [vba]
    If Userform1.Textbox1.value = "xxx" Then Userform1.Textbox1.BackColor = RGB(xxx, xxx, xxx)
    [/vba]
    You could use a loop or "For Each" and then set if or case statements for the values. That should reduce the code quit a bit.

    HTH,
    Roger

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,438
    Location
    Private Sub TextBox1_Change()
        Call TB_Colour(Me.TextBox1)
    End Sub
    
    Private Sub TextBox2_Change()
        Call TB_Colour(Me.TextBox2)
    End Sub
    
    'etc.
    
    Private Sub TB_Colour(ByRef tb As MSForms.TextBox)
    Select Case tb.Value
        Case "xxx": tb.BackColor = RGB(xxx, xxx, xxx)
        Case "yyy": tb.BackColor = RGB(xxx, yyy, xxx)
        Case "zzz": tb.BackColor = RGB(xxx, xxx, zzz)
        'etc.
        End Select
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Nice.

  5. #5
    VBAX Regular rrtts's Avatar
    Joined
    Sep 2006
    Posts
    61
    Location
    That is genius. Works briliiantly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •