Form button to control checkbox selection

fergyz

New Member
Joined
Jan 30, 2018
Messages
7
I have what should be a simple piece of VBA code to put together, but for the life of me i am hitting a brick wall. i am taking a VBA class but the prof is having us write code that isn't in the book or maybe we haven't covered yet.

i have a (form) button that needs to be used to toggle a single checkbox with in the same sheet. the checkbox applies a conditional formatting when selected.

This is the only code i have been able to get to work half-right. i realize it was meant for multiple check boxes but i tried to tailor it to a single.

Sub Hk()

Dim chkBox As Excel.CheckBox
Application.ScreenUpdating = False
For Each chkBox In ActiveSheet.CheckBoxes
chkBox.Value = xlOn
Next chkBox
Application.ScreenUpdating = True
End Sub


i still think there is something even simpler - using the form vs activex button

Thanx
 

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.
Change the name of the checkbox to suit

Code:
Sub Hk()
    With ActiveSheet.CheckBoxes("[COLOR=#ff0000]Check Box 2[/COLOR]")
        If .Value = xlOff Then .Value = xlOn Else .Value = xlOff
    End With
End Sub

Or this...
Code:
Sub Hk()
    With ActiveSheet.CheckBoxes("[COLOR=#ff0000]Check Box 2[/COLOR]")
        .Value = IIf(.Value = xlOff, xlOn, xlOff)
    End With
End Sub
 
Last edited:
Upvote 0
Change the name of the checkbox to suit

Code:
Sub Hk()
    With ActiveSheet.CheckBoxes("[COLOR=#ff0000]Check Box 2[/COLOR]")
        If .Value = xlOff Then .Value = xlOn Else .Value = xlOff
    End With
End Sub

Or this...
Code:
Sub Hk()
    With ActiveSheet.CheckBoxes("[COLOR=#ff0000]Check Box 2[/COLOR]")
        .Value = IIf(.Value = xlOff, xlOn, xlOff)
    End With
End Sub

Or this (assuming the CheckBox is initialized with either xlOn or xlOff to start with)...
Code:
Sub Hk()
    With ActiveSheet.CheckBoxes("[COLOR=#ff0000]Check Box 2[/COLOR]")
        .Value = (xlOn + xlOff) - .Value
    End With
End Sub
 
Last edited:
Upvote 0
Thank you very much. i knew it was a short simple one. i was going to resort to a msgbox to trigger the checkbox.
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

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