VBA to change Form Control Checkbox background color when checked

FiservEFTSalesOps

New Member
Joined
Dec 7, 2009
Messages
31
I have an Excel 2010 workbook with many worksheets using hundreds of Form Control (not ActiveX) checkboxes. I need a bit of VBA to change Checkbox background color of each checkbox whenever the user checks the box. I assume this needs to be a click event? I don't even know the Checkbox property name I need to change :(
I'm learning VBA as quick as I can, but need some assistance.

Thank you all.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
It makes a big difference if the check boxes already have a macro assigned (OnAction parameter in properties). Here's some code to assign an OnAction if the checkboxes don't have one.



Make some form check boxes on a sheet
Copy this code into a module (alt F11, insert, module)
run SetMacro
Save and test


Code:
Sub SetMacro()
    Dim cb
    For Each cb In ActiveSheet.CheckBoxes
        If cb.OnAction = "" Then cb.OnAction = "CheckedUnchecked"
    Next cb
End Sub
Sub CheckedUnchecked()
    With ActiveSheet.Shapes(Application.Caller).DrawingObject
        If .Value = 1 Then
            .Interior.ColorIndex = 4
        Else
            .Interior.ColorIndex = 2
        End If
    End With
End Sub

If there are macros assigned, then the macros need editing.
 
Upvote 0
Replace previous code, and run SetMacro

Code:
Sub SetMacro()
    Dim cb, ws
    For Each ws In ThisWorkbook.Sheets
        For Each cb In ws.CheckBoxes
            If cb.OnAction = "" Then cb.OnAction = "CheckedUnchecked"
        Next cb
    Next ws
End Sub
Sub CheckedUnchecked()
    With ActiveSheet.Shapes(Application.Caller).DrawingObject
        If .Value = 1 Then
            .Interior.ColorIndex = 4
        Else
            .Interior.ColorIndex = 2
        End If
    End With
End Sub
 
Upvote 0
It makes a big difference if the check boxes already have a macro assigned (OnAction parameter in properties). Here's some code to assign an OnAction if the checkboxes don't have one.



Make some form check boxes on a sheet
Copy this code into a module (alt F11, insert, module)
run SetMacro
Save and test


Code:
Sub SetMacro()
    Dim cb
    For Each cb In ActiveSheet.CheckBoxes
        If cb.OnAction = "" Then cb.OnAction = "CheckedUnchecked"
    Next cb
End Sub
Sub CheckedUnchecked()
    With ActiveSheet.Shapes(Application.Caller).DrawingObject
        If .Value = 1 Then
            .Interior.ColorIndex = 4
        Else
            .Interior.ColorIndex = 2
        End If
    End With
End Sub

If there are macros assigned, then the macros need editing.


What macro code changes are required to use RGB instead of ColorIndex?</SPAN>

Thank you so much..</SPAN>
 
Upvote 0
Code:
Sub SetMacro()
    Dim cb, ws
    For Each ws In ThisWorkbook.Sheets
        For Each cb In ws.CheckBoxes
            If cb.OnAction = "" Then cb.OnAction = "CheckedUnchecked"
        Next cb
    Next ws
End Sub
Sub CheckedUnchecked()
    With ActiveSheet.Shapes(Application.Caller).DrawingObject
        If .Value = 1 Then
            [COLOR=#00ff00][B].Interior.Color = RGB(0, 255, 0)[/B][/COLOR]
        Else
            [COLOR=#00ff00][B].Interior.Color = RGB(255, 255, 255)[/B][/COLOR]
        End If
    End With
End Sub
 
Upvote 0
If you assign a Forms Control Checkbox to this macro, the background color of the checkbox will change between Red and Green, depending on whether the user check or un-checks the box.
Note that several different checkboxes can be assigned to this macro (or if they call separate macros, those can call this) and it will work.

Code:
Sub CheckBox1_Click()
    With ActiveSheet.Shapes(Application.Caller)
        If .ControlFormat.Value = xlOn Then
            With .Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
            End With
        Else
            With .Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(0, 255, 0)
            End With
        End If
    End With
End Sub
 
Upvote 0
Code:
Sub SetMacro()
    Dim cb, ws
    For Each ws In ThisWorkbook.Sheets
        For Each cb In ws.CheckBoxes
            If cb.OnAction = "" Then cb.OnAction = "CheckedUnchecked"
        Next cb
    Next ws
End Sub
Sub CheckedUnchecked()
    With ActiveSheet.Shapes(Application.Caller).DrawingObject
        If .Value = 1 Then
            [COLOR=#00ff00][B].Interior.Color = RGB(0, 255, 0)[/B][/COLOR]
        Else
            [COLOR=#00ff00][B].Interior.Color = RGB(255, 255, 255)[/B][/COLOR]
        End If
    End With
End Sub


Thank you for your help, code changes work OK.</SPAN>
 
Upvote 0
If you assign a Forms Control Checkbox to this macro, the background color of the checkbox will change between Red and Green, depending on whether the user check or un-checks the box.
Note that several different checkboxes can be assigned to this macro (or if they call separate macros, those can call this) and it will work.

Code:
Sub CheckBox1_Click()
    With ActiveSheet.Shapes(Application.Caller)
        If .ControlFormat.Value = xlOn Then
            With .Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
            End With
        Else
            With .Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(0, 255, 0)
            End With
        End If
    End With
End Sub


Thank you for the macro. Works as you said without any problems with several check boxes.</SPAN>

Please explain what the “x10n” refers to? Is it the check mark inside the check box?</SPAN>
What is the difference between “= msoTrue” and “= True”?</SPAN>
 
Upvote 0

Forum statistics

Threads
1,223,150
Messages
6,170,377
Members
452,323
Latest member
robertbs021

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