Help with checkbox and macro

dee101

Active Member
Joined
Aug 21, 2004
Messages
282
I want to have a check box on a sheet and when I run a macro I want it to do different things depending if the check box is check or not, I do NOT want this in the check box click event but some how when the macro is run it will get the valve of the check box and do something depending on its valve. Make any sense? Something like this is what I have in mind. Will the check box need to be from the forms or control toolbox?
Thanks

Sub test()
If CheckBox1.Value = True Then

'do something here
MsgBox "true"

ElseIf CheckBox1.Value = False Then

'do something else here
MsgBox "false"

End If
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The code that you posted will work fine.

You would need to use it with an ActiveX control from the ControlToolbox.

Although you'll need to add a bit to get it to work with a worksheet control:

<font face=tahoma><SPAN style="color:#00007F">Sub</SPAN> foo()
    <SPAN style="color:#00007F">If</SPAN> ActiveSheet.CheckBox1.Value = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#007F00">'do something here</SPAN>
        MsgBox "true"
    <SPAN style="color:#00007F">ElseIf</SPAN> ActiveSheet.CheckBox1.Value = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#007F00">'do something else here</SPAN>
        MsgBox "false"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Hope that helps,

Smitty
 
Upvote 0
If you have a number of checkboxes you can use this code.

Sub test()
Dim ctrl As Control
For I = 1 To 10
With Controls("CheckBox" & I)
If .Value = True Then
leave_type = I
Exit For
End If
End With
Next I

End Sub
 
Upvote 0
It will check a series of Checkboxes on a form so you can run a procedure reatling to what ever you have you checkbox set to do, and what ever the user selects.
 
Upvote 0

Forum statistics

Threads
1,203,521
Messages
6,055,883
Members
444,830
Latest member
Excelsmallbusinessmom

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