Checking if a worksheet-based checkbox is checked
Sub Button167_Click()
If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
Range("Y12").Value = 1
Else
Range("Y12").Value = 0
End If
End Sub
1 is checked, -4146 is unchecked, 2 is mixed (grey box)
It seems that in VBA macro code for an ActiveX checkbox control, you use
If (ActiveSheet.OLEObjects("CheckBox1").Object.Value = True)
and for a Form checkbox control, you use
If (ActiveSheet.Shapes("CheckBox1").OLEFormat.Object.Value = 1)
Building on the previous answers, you can leverage the fact that True
is -1 and False
is 0 and shorten your code like this:
Sub Button167_Click()
Range("Y12").Value = _
Abs(Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0)
End Sub
If the checkbox is checked, .Value
= 1.
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0
returns True
.
Applying the Abs
function converts True
to 1
.
If the checkbox is unchecked, .Value
= -4146.
Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0
returns False
.
Applying the Abs
function converts False
to 0
.
Is this what you are trying?
Sub Sample()
Dim cb As Shape
Set cb = ActiveSheet.Shapes("Check Box 1")
If cb.OLEFormat.Object.Value = 1 Then
MsgBox "Checkbox is Checked"
Else
MsgBox "Checkbox is not Checked"
End If
End Sub
Replace Activesheet
with the relevant sheetname. Also replace Check Box 1
with the relevant checkbox name.