Consulting

Results 1 to 8 of 8

Thread: Change Textbox and Combobox Back Color

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    37
    Location

    Change Textbox and Combobox Back Color

    Hi-
    I am trying to change Textbox and Combobox back color. I tried using the code below but nothing happens!
    I like to apply the background color change to all my Textboxes and Combobox on the userform.

    To change textbox:
    Private Sub textbox3_Change()
    textbox3.BackColor = RGB(75, 116, 71) 'Dark Green
    End Sub
    Thanks.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Private Sub Test()
    For Each ctrl In UserForm1.Controls
    If UCase(TypeName(ctrl)) = "TEXTBOX" Or UCase(TypeName(ctrl)) = "COMBOBOX" Then
        ctrl.BackColor = RGB(75, 116, 71)
    End If
    Next
    UserForm1.Show False
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Sep 2016
    Posts
    37
    Location
    mdmackillop,
    I just gave this a try but, nothing?
    I even created a new textbox to see if it would work but, no change.

    Nimesh

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post your workbook?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Both work for me.
    Private Sub UserForm_Initialize()  
      Dim ctrl As Control
        For Each ctrl In UserForm1.Controls
          If UCase(TypeName(ctrl)) = "TEXTBOX" Or UCase(TypeName(ctrl)) = "COMBOBOX" Then
            ctrl.BackColor = RGB(75, 116, 71)
          End If
        Next
    End Sub
    
    
    Private Sub textbox3_Change()
        TextBox3.BackColor = RGB(75, 116, 71) 'Dark Green
    End Sub

  6. #6
    VBAX Regular
    Joined
    Sep 2016
    Posts
    37
    Location
    Kenneth-
    That worked, the only difference I see is from mdmackillop code is "Userform1.show" line.

    Quick question: Would I be able to add "label' to control font style and color to this code?

    Thank You.

    Last edited by nimesh29; 09-22-2017 at 06:19 AM.

  7. #7
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Private Sub UserForm_Initialize()  
      Dim ctrl As Control
      For Each ctrl In UserForm1.Controls
        With ctrl
          Select Case UCase(TypeName(ctrl))
            Case "TEXTBOX", "COMBOBOX"
              .BackColor = RGB(75, 116, 71)
            Case "LABEL"
              .BackColor = vbRed
              With .Font
                .Name = "Times New Roman"
                '.FontStyle = "Bold Italic" 'No, errors
                .Bold = True
                .Italic = True
                .Size = 12
              End With
            Case Else
          End Select
        End With
      Next ctrl
    End Sub

  8. #8
    VBAX Regular
    Joined
    Sep 2016
    Posts
    37
    Location
    WOW! Thank you!

    Nimesh

Tags for this Thread

Posting Permissions

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