Change Toggle Command Button Back Color

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
180
Office Version
  1. 2021
Platform
  1. Windows
I have a "toggle" command button, and it works great.
The only thing missing is that I'd like the button color to toggle from Red to Green.
Here is my VBA Code, note that I tried to place the code ".BackColor = vbRed" and ".BackColor = vbGreen" in various places within the macro.
I commented them out because I keep getting a Debug message.
Thank you.

VBA Code:
Private Sub ToggleMacro()
 
'Simplify code by refering to object once
With ActiveSheet.Shapes("Button1").TextFrame.Characters
 
'Check if button text is equal to a specific string.
If .Text = "VOID Check" Then

    Dim ws As Worksheet
    Dim rng As Range
    Set ws = Sheets("Sheet1")
    Set rng = ws.Range("K16:K19")

[B]'    Button1.BackColor = vbRed[/B]
    With ws.Shapes("Picture 3")
        .LockAspectRatio = msoFalse
        .Top = rng.Top
        .Left = rng.Left
        .Height = rng.Height
        .Width = rng.Width
[B]'        .BackColor = vbRed[/B]
    End With


'Change button text.
    .Text = "Do Not VOID Check"
[B]'    .BackColor = vbRed[/B]
'This happens if button text is not equal to the specific string.
Else
    Set ws = Sheets("Sheet1")
    Set rng = ws.Range("B16:d20")
    
    With ws.Shapes("Picture 3")
        .LockAspectRatio = msoFalse
        .Top = rng.Top
        .Left = rng.Left
        .Height = rng.Height
        .Width = rng.Width
[B]'        .BackColor = vbGreen[/B]
    End With

'Change button text.
    .Text = "VOID Check"
[B]'    .BackColor = vbGreen[/B]
End If
 
End With
 
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It is easier to do with an ActiveX command button control than it is with a form button
VBA Code:
Private Sub CommandButton3_Click()
    With CommandButton3
        Select Case .BackColor
        Case vbRed
            .BackColor = vbGreen
        Case Else
            .BackColor = vbRed
        End Select
    End With
End Sub
 
Upvote 0
You cannot change the colour of a Form Control button, but you could use a shape instead.
 
Upvote 0
Fluff,
I think I figured it out.
I'd like to send you my whole XLSM file, but I'm not sure how to do that.
The purpose is to move an image "VOID" around.
The Button is named "Button1" and the image is named "Picture 3".
Please try it and let me know what you think.
Feel free to "clean it up" if my wording is messy.
Thanks.

Here is my macro.

VBA Code:
Private Sub ToggleMacro()
 
'Simplify code by refering to object once
With ActiveSheet.Shapes("Button1").TextFrame.Characters
 
'Check if button text is equal to a specific string.
If .Text = "VOID Check" Then
    With ActiveSheet.Shapes("Button1").Fill
        .ForeColor.RGB = RGB(255, 0, 0)
    End With

    Dim ws As Worksheet
    Dim rng As Range
    Set ws = Sheets("Sheet1")
    Set rng = ws.Range("K16:K19")

'    Button1.BackColor = vbRed
    With ActiveSheet.Shapes("Button1").Fill
        .ForeColor.RGB = RGB(255, 0, 0)
    End With

    With ws.Shapes("Picture 3")
        .LockAspectRatio = msoFalse
        .Top = rng.Top
        .Left = rng.Left
        .Height = rng.Height
        .Width = rng.Width
    End With
    

    'Change button text.
    .Text = "Do Not VOID Check"

'This happens if button text is not equal to the specific string.
Else
    Set ws = Sheets("Sheet1")
    Set rng = ws.Range("B16:d20")
    
    With ActiveSheet.Shapes("Button1").Fill
        .ForeColor.RGB = RGB(51, 153, 102)
    End With
    
    With ws.Shapes("Picture 3")
        .LockAspectRatio = msoFalse
        .Top = rng.Top
        .Left = rng.Left
        .Height = rng.Height
        .Width = rng.Width
    End With

    'Change button text.
    .Text = "VOID Check"
End If
 
End With
 
End Sub
 
Upvote 0
Does it work?
YES !!!!
I wanted the button color to toggle from Green to Red using an IF Statement dependent upon the text on the button.
When you told me that I cannot change the color of a Form Control Button, I decided to try to change the color of the text on the button to toggle from Green to Red.
So instead of BackColor, I started playing around with ForeColor.
Turns out, the ForeColor changes the color of the button !!!
Now, I'm not sure how to change the color of the text on the button. :)
Anyway, try these lines of code and please let me know how it works for you.

With ActiveSheet.Shapes("Button1").Fill
.ForeColor.RGB = RGB(160, 0, 0)
End With

With ActiveSheet.Shapes("Button1").Fill
.ForeColor.RGB = RGB(0, 160, 0)
End With

Here is my FINAL VBA Code:

VBA Code:
Sub ToggleMacro()
 
    Dim ws As Worksheet
    Dim rng As Range
    Set ws = Sheets("Printed_Check")
    Set rng = ws.Range("K16:K19")
    
'Simplify code by refering to object once
With ActiveSheet.Shapes("Button 1").TextFrame.Characters
 
'Check if button text is equal to a specific string.
If .Text = "VOID Check" Then
    With ActiveSheet.Shapes("Button 1").Fill
        .ForeColor.RGB = RGB(160, 0, 0)
    End With

    With ws.Shapes("Picture 3")
        .LockAspectRatio = msoFalse
        .Top = rng.Top
        .Left = rng.Left
        .Height = rng.Height
        .Width = rng.Width
    End With

    'Change button text.
    .Text = "Do Not VOID Check"
'This happens if button text is not equal to the specific string.
Else
    Set ws = Sheets("Printed_Check")
    Set rng = ws.Range("P16:P20")
    
    With ActiveSheet.Shapes("Button 1").Fill
        .ForeColor.RGB = RGB(0, 160, 0)
    End With
    
    With ws.Shapes("Picture 3")
        .LockAspectRatio = msoFalse
        .Top = rng.Top
        .Left = rng.Left
        .Height = rng.Height
        .Width = rng.Width
    End With

    'Change button text.
    .Text = "VOID Check"
End If
 
End With
 
End Sub
 
Upvote 0
For a form control button:
VBA Code:
Sub ToggleMacro()
    With ActiveSheet.Buttons("Button1")
        If .Caption = "VOID Check" Then
            .Font.Color = vbRed
            .Caption = "Do Not VOID Check"
        Else
            .Font.Color = vbGreen
            .Caption = "VOID Check"
        End If
    End With
End Sub
 
Upvote 0
Hey Jaafar Tribak
Thanks, but .... when I try to run your macro, I get this error message:

Screenshot 2023-02-27 204013.png
 
Upvote 0
For a form control button:
VBA Code:
Sub ToggleMacro()
    With ActiveSheet.Buttons("Button1")
        If .Caption = "VOID Check" Then
            .Font.Color = vbRed
            .Caption = "Do Not VOID Check"
        Else
            .Font.Color = vbGreen
            .Caption = "VOID Check"
        End If
    End With
End Sub
Just noting that you can replace your seven line If..End If block with these two lines of code...
VBA Code:
.Font.Color = vbRed + vbGreen - .Font.Color
.Captions = Mid("Do Not VOID Check", 1 - 3 * (Left(.Caption, 2) = "Do") )
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top