VBA - Check Box and multiple macros

candacem

New Member
Joined
Sep 8, 2017
Messages
18
Hi Everyone,

I have two macros to change formatting and formulas and want to assign them to a Forms Control check box. I want one macro to run when checked and the other when unchecked.

Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Give this a try.

To test it out, copy the three macros to a standard module, and assign Sub Macro_On_Off_Click() to your check box (right click the ck box "Assign Macro")
Click the box to check ON and Box_Code_ON runs to simulate the formatting code for click ON.
Click the box to check OFF and Box_Code_OFF runs to simulate the formatting code for click OFF.

Howard


Code:
Sub Macro_On_Off_Click()
    
    With ActiveSheet.Shapes(Application.Caller)
       
        If .ControlFormat = xlOn Then
           Box_Code_ON
          Else
           Box_Code_OFF
        End If
        
    End With
    
End Sub

Sub Box_Code_ON()
MsgBox "Format code for box runs when clicked ON"
End Sub

Sub Box_Code_OFF()
MsgBox "Format code for box runs when clicked OFF"
End Sub
 
Upvote 0
Alternatively you could just use 1 macro
Code:
Sub ChkBx()

    Dim Chk As CheckBox

    Set Chk = ActiveSheet.CheckBoxes(Application.Caller)

    If Chk = 1 Then
        Range("A1").Interior.Color = vbRed
    Else
        Range("A1").Interior.Color = vbBlue
    End If
    

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,656
Latest member
earth

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