+ Reply to Thread
Results 1 to 9 of 9

change command button colour when selected

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    365
    Posts
    418

    change command button colour when selected

    Good Morning

    I have several forms with multiple command buttons... all have different names however they all start with 'cmd'

    Is there a way for a button to change colour when selected whilst changing the rest of the buttons on that form to their origional colour... I know how to do it, using this, when the buttons have numbers instead of names....

    Many thanks for your help

    Jim
    For x = 1 To 18
            Me.Controls("cmd" & x).BackColor = &HFFFFFF 'sorts the main cmd button backcolours, light grey
            cmd1.BackColor = RGB(51, 204, 255) 'selected button turns blue
        Next
    Attached Files Attached Files
    Last edited by JamesT1; 10-02-2023 at 07:50 AM.

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,313

    Re: change command button colour when selected

    see large yellow banner - how to upload your workbook - far easier then to give you a tailor made solution.
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  3. #3
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    365
    Posts
    418

    Re: change command button colour when selected

    Thanks torachan

    The attament shows what I am after,, the number buttons change colour when selected with the others returning to their origional colours,,,,.. I'm looking to do this when the buttons have names

    Many thanks

    jim

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,701

    Re: change command button colour when selected

    One way:

    Private Sub cmddothis_Click()
    For Each cmd In Me.Controls
        If cmd.Caption Like "Names *" Then
            Debug.Print cmd.Caption
            cmd.BackColor = &HFFFFFF 'sorts the main cmd button backcolours, light grey
        End If
        cmddothis.BackColor = RGB(51, 204, 255) 'selected button turns blue
    Next
    
    End Sub
    
    Private Sub cmddothat_Click()
    For Each cmd In Me.Controls
        If cmd.Caption Like "Names *" Then
            Debug.Print cmd.Caption
            cmd.BackColor = &HFFFFFF 'sorts the main cmd button backcolours, light grey
        End If
        cmddothat.BackColor = RGB(51, 204, 255) 'selected button turns blue
    Next
    
    End Sub
    
    'etc
    'etc
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,701

    Re: change command button colour when selected

    Or, you could add a Tag to all the Names buttons and check that. For example:
            Debug.Print cmd.Caption, cmd.Tag

  6. #6
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    365
    Posts
    418

    Re: change command button colour when selected

    TMS,,, your 1st one does what I need.. many thanks for your help,,,

    Not sure how tags work just now, so will have a look at that later

    Many thanks

    For your help.. appreciated

    Jim

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,701

    Re: change command button colour when selected

    You're welcome. Thanks for the rep.

    Tag works much the same as caption but you could, for example, make the Name buttons tagged as Name and all the Number buttons tagged as Number. Then you can use the same loop structure.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,701

    Re: change command button colour when selected

    This would probably be a more effective and less repetitive method of highlighting the selected command button(s).

    Option Explicit
    
    ' The code below assume that:
    ' Number Command Buttons have the Tag set to "Number" and
    ' Name Command Buttons have the Tag set to "Name"
    
    Private Sub cmd1_Click()
    cmd_Check_State cmd1.Name, cmd1.Tag
    End Sub
    
    Private Sub cmd2_Click()
    cmd_Check_State cmd2.Name, cmd2.Tag
    End Sub
    
    Private Sub cmd3_Click()
    cmd_Check_State cmd3.Name, cmd3.Tag
    End Sub
    
    Private Sub cmddothis_Click()
    cmd_Check_State cmddothis.Name, cmddothis.Tag
    End Sub
    
    Private Sub cmddothat_Click()
    cmd_Check_State cmddothat.Name, cmddothat.Tag
    End Sub
    
    Private Sub cmdonejob_Click()
    cmd_Check_State cmdonejob.Name, cmdonejob.Tag
    End Sub
    
    Private Sub cmdyourjob_Click()
    cmd_Check_State cmdyourjob.Name, cmdyourjob.Tag
    End Sub
    
    Private Sub cmdanyonesjob_Click()
    cmd_Check_State cmdanyonesjob.Name, cmdanyonesjob.Tag
    End Sub
    
    Private Sub cmd_Check_State(cmdButton, cmdTag As String)
    Dim cmd
    For Each cmd In Me.Controls
        If cmd.Tag = cmdTag Then
            cmd.BackColor = &HFFFFFF ' sets the main cmd button backcolours, light grey
        End If
    Next
    Me.Controls(cmdButton).BackColor = RGB(51, 204, 255) ' selected button turns blue
    End Sub
    Attached Files Attached Files

  9. #9
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,313

    Re: change command button colour when selected

    You could use a class module to intercept the MouseDown and MouseUp conditions - with this method you do not have to intercept each Click event.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 6
    Last Post: 08-31-2017, 12:16 PM
  2. need a command button to change / unchange the colour of specified cells
    By fingermouse in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-09-2014, 09:36 AM
  3. [SOLVED] Command button grayed out till option button selected
    By Jeff121 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2013, 11:00 AM
  4. using a command button to change certain cells font colour.
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-17-2013, 10:08 AM
  5. [SOLVED] Change Command Button Colour Based on Cell
    By adam.hewitt5 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-08-2012, 09:43 AM
  6. Colour change of the "Command Button"
    By Johann 12081 in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 07-21-2009, 01:24 PM
  7. command button that changes colour of selected cells
    By BLRITCHIE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2008, 07:33 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1