Consulting

Results 1 to 6 of 6

Thread: CommandButton Back Colour is not set on WorkBook Open

  1. #1

    Question CommandButton Back Colour is not set on WorkBook Open

    Hi All,
    I am new to this forum so please accept my apologies if this has been asked before. In my workbook open process I have a line of VBA code to set the backcolour of one of my worksheet command buttons to indicate that it is active (to distinguish it from the other buttons). The code is invoked but the button colour is not altered. However, if I then click on the same button the exact same code is invoked to set the backcolour and the colour of the button changes straightaway i.e. as soon as the code is executed.

    The only thing I can surmise is that the button colour has not changed during "start up" because the button has not actually been physically clicked. ?

    Thanks Craig

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Show the code in question.
    ____________________________________________
    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

  3. #3
    Hi Bob,
    The start=up code invokes the table Change procedure

    Public Sub Worksheet_Activate()
         
        buttonActiveColour = RGB(115, 135, 156)
        If (currentTable = "") Or (currentTable = "Bookings") Then
            currentTable = "Bookings"
            tableChange
        Else
            tableChange
        End If
    
    
    End Sub

    This is the tableChange procedure. On startup the backcolor function does nothing but when the procedure is invoked from a button click event the back colour changes as you would expect.

    Private Sub tableChange()
       
        Dim tableWidth As Long
        Dim i As Integer
        Dim currentOnly As Boolean
        
        ClearScreen
    
    
        Select Case currentTable
            Case Is = "Bookings"
                cmdBooking.BackColor = buttonActiveColour
    Last edited by Bob Phillips; 01-24-2022 at 09:25 AM. Reason: Added code tags

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Doesn't clear up too much for me. Unless buttonActiveColour is defines as a global variable, the variable in the Activate event and in the tableChange are different variables, each local to that procedure.

    The same applies to currentTable as to buttonActiveColour.

    Are you sure that currentTable is Bookings in that Activate event?
    ____________________________________________
    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

  5. #5
    Hi Bob,
    Yes, both buttonActiveColour and currentTable are global variables. I have debugged the code on startup and the currentTable is set to Bookings and the tableChange event is triggered from the Activate event, and then in turn the line of code that sets the button's backColour is triggered but it has no affect. However, if the tableChange procedure is triggered by a user actually clicking on a CommandButton the nthe back colour changes.

    The use of another procedure is really a red herring as I have seen the same issue even if I code everything within the Activate event i.e. I originally coded the Activate event to set the back colour of the button directly to an RGB value (as opposed to a variable) again nothing; although in the same block of code I amended the caption property which did change. So, essentially I have proved that I can change the commandButton caption within the WorkSheet Activate event but NOT the backColour .. very ODD.

    Thanks again, Craig

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I couldn't get the snippets of code to work so I changed stuff for testing

    I think that this layout is a little more reliable, and has the advantage that it seems to work


    In ThisWortkbook

    Option Explicit
    
    
    Private Sub Workbook_Open()
        buttonActiveColour = vbGreen        '   RGB(115, 135, 156)
        buttonInactiveColour = vbRed        '   RGB(225, 225, 225)
        
        tableChange
    End Sub

    In a Standard Module

    Option Explicit
    
    
    Public buttonActiveColour As Long
    Public buttonInactiveColour As Long
    Public currentTable As String
    
    
    Sub tableChange()   '   not Private
        
        With Worksheets("Sheet1")
            
            'for testing
            currentTable = .Range("K3").Value
       
            Select Case currentTable
                Case Is = "Bookings"
                    .cmdBooking.BackColor = buttonActiveColour
                Case Else
                    .cmdBooking.BackColor = buttonInactiveColour
            End Select
        End With
    
    
    End Sub

    In the Worksheet Module

    Option Explicit
    
    
    Private Sub cmdBooking_Click()
        tableChange
    End Sub
    
    
    Private Sub Worksheet_Activate()
        tableChange
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

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
  •