ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check box making a command button work (https://www.excelbanter.com/excel-programming/345417-check-box-making-command-button-work.html)

Rmagic

Check box making a command button work
 

Hello

I have a check box on a sheet from the forms toolbar
I would like to make a command button work only when the check box is
ticked

Thank you


--
Rmagic
------------------------------------------------------------------------
Rmagic's Profile: http://www.excelforum.com/member.php...o&userid=28595
View this thread: http://www.excelforum.com/showthread...hreadid=484465


JakeyC

Check box making a command button work
 
Insert the following at the beginning of your CommandButton1_click
code:

If CheckBox1.Value = True Then
Exit Sub
End If

This way, the sub will end before anything gets done.


JakeyC

Check box making a command button work
 
Apologies - my code will run when the box is UNchecked! Change 'True'
to 'False' as needed...


Dave Peterson

Check box making a command button work
 
I used a button from the forms toolbar--is that what you meant?

You could either add some code to the button's code:

Option Explicit
Sub btnClick()

If ActiveSheet.CheckBoxes("Check box 1").Value = xlOff Then
MsgBox "nope, not today!"
Exit Sub
End If

'your real code here

MsgBox "it's working"

End Sub

Or you could disable the button (or hide it) when the checkbox changes:

Option Explicit
Sub CBXClick()
ActiveSheet.Buttons("button 1").Enabled _
= CBool(ActiveSheet.CheckBoxes("Check box 1").Value = xlOn)
'or hide it???
ActiveSheet.Buttons("button 1").Visible _
= CBool(ActiveSheet.CheckBoxes("Check box 1").Value = xlOn)
End Sub

If you really meant a commandbutton from the control toolbox toolbar, something
like:

Sub CBXClick()
ActiveSheet.CommandButton1.Enabled _
= CBool(ActiveSheet.CheckBoxes("Check box 1").Value = xlOn)
'or to hide it
ActiveSheet.CommandButton1.Visible _
= CBool(ActiveSheet.CheckBoxes("Check box 1").Value = xlOn)
End Sub

But the same kind of check inside the commandbutton's code would work ok (like
in btnclick procedure).


Rmagic wrote:

Hello

I have a check box on a sheet from the forms toolbar
I would like to make a command button work only when the check box is
ticked

Thank you

--
Rmagic
------------------------------------------------------------------------
Rmagic's Profile: http://www.excelforum.com/member.php...o&userid=28595
View this thread: http://www.excelforum.com/showthread...hreadid=484465


--

Dave Peterson


All times are GMT +1. The time now is 08:48 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com