Check a Check Box with VB

K

kittronald

What is the VB macro code to "check" a Check Box forms control ?



- Ronald K.
 
K

kittronald

Dave,

Perhaps I'm not using the proper syntax.

Sheet2.CheckBox23.Value = True



- Ronald K.
 
K

kittronald

Dave,

Thanks for the quick response.

Apparently, pressing the Send button was the solution.

Sheet2.CheckBoxes("Check Box 23").Value = True

In the future, I'll press the Send button before posting a question. 8)




- Ronald K.
 
K

kittronald

Is this the shortest way to test if a Checkbox is already checked ?

Sub TEST()
If Sheet2.CheckBoxes("Check Box 23").Value = False Then
Sheet2.CheckBoxes("Check Box 23").Value = True
Else
Sheet2.CheckBoxes("Check Box 23").Value = True
End If
End Sub



- Ronald K.
 
G

GS

kittronald expressed precisely :
Is this the shortest way to test if a Checkbox is already checked ?

Sub TEST()
If Sheet2.CheckBoxes("Check Box 23").Value = False Then
Sheet2.CheckBoxes("Check Box 23").Value = True
Else
Sheet2.CheckBoxes("Check Box 23").Value = True
End If
End Sub



- Ronald K.

No need to make the If evaluation redundant, it just adds extra
processing for VBA. No big deal on one-by-one basis but the cummulative
effect can add up...

Sub TEST()
If Not Sheet2.CheckBox23 Then Sheet2.CheckBox23 = True
End Sub

...so if it's NOT True then it gets changed to True. No need to set it
to True if it's already True! No need to test if its value = False
because Value is its default property. IOW, the control can only be
True (checked) or False (unchecked).
 
D

Dave Peterson

This looks like it's a checkbox from the Forms toolbar -- not from the control
toolbox toolbar.

You can use xloff or xlon.

And if you just want to make it checked, there's no reason to check first.

sheet2.checkboxes("check box 23").value = xlon

If you are processing something based on the checkbox, you could use:

if sheet2.checkboxes("check box 23").value = xlon then
'or = xloff to check to see if it's empty
 
K

kittronald

Garry,

Got it to work with a little modification.

If Not Sheet2.CheckBoxes("Check Box 23") Then
Sheet2.CheckBoxes("Check Box 23") = True

Thanks again.



- Ronald K.
 
K

kittronald

Dave,

The Check Box is from the Forms toolbar.

If the Check Box was already checked, would repeatedly setting the Check
Box to xlon trigger events to execute again that are dependent on it being
checked ?



- Ronald K.
 
G

GS

After serious thinking kittronald wrote :
Garry,

Got it to work with a little modification.

If Not Sheet2.CheckBoxes("Check Box 23") Then
Sheet2.CheckBoxes("Check Box 23") = True

Thanks again.



- Ronald K.

Ok, Ron. So as Dave suggests you are using Forms controls and not
ActiveX. My bad for not picking up on this. Glad you got it working...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top