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
 
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") )
Not intuitive but slick and clever ... Thanks.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If you are using a shape rather than a button you can use
VBA Code:
    With ActiveSheet.Shapes("Button 1")
        .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 255, 255)
        .Fill.ForeColor.RGB = RGB(160, 0, 0)
    End With
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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