I'm trying to use an IF-clause to determine whether my checkbox, named "Check Box 1", is checked.
My current code:
Sub Button167_Click()
If ActiveSheet.Shapes("Check Box 1") = True Then
Range("Y12").Value = 1
Else
Range("Y12").Value = 0
End If
End Sub
This doesn't work. The debugger is telling me there is a problem with the
ActiveSheet.Shapes("Check Box 1")
However, I know this code works (even though it serves a different purpose):
ActiveSheet.Shapes("Check Box 1").Select
With Selection
.Value = xlOn
My checkboxes (there are 200 on my page), are located in sheet1, by the name of "Demande". Each Checkbox is has the same formatted name of "Check Box ...".
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)
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.
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.
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)
Try: Controls("Check Box 1") = True
Related
Situation
I have two form control check boxes. I am trying to write a code that will allow only either of them to be true.
my code is
Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox2.Enabled = False
Else
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
End If
End If
End Sub
I have this code in module and have assigned the same macro for both the checkboxes. I get run-time error 424. I beleive this is very basic problem but I unable to dela with it.
Thank you
Please, test the following way. Form check boxes do not have a click event, as ActiveX ones have. You should associate the next sub to both of them. The check boxes I tested, have their names as "Check Box 1" and "Check Box 2". You have to change yours according to the reality in your case, Please, copy the next code in a standard module and then associate it to both used check boxes:
Option Explicit
Sub FormCheckBoxChange()
If ActiveSheet.CheckBoxes(Application.Caller).value = 1 Then
Select Case Application.Caller
Case "Check Box 1": ActiveSheet.CheckBoxes("Check Box 2").value = -4146
Case "Check Box 2": ActiveSheet.CheckBoxes("Check Box 1").value = -4146
End Select
End If
End Sub
Use instead of the used check box names, the appropriate ones for your case.
In case of Form text boxes, their value is not True and False as in case of ActiveX ones. It is 1 and -4146...
Are you sure you want to enable/disable the checkboxes.
Following code makes sure that either one of both boxes is checked.
Public Sub checkbox2_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb2.Value = 1 Then oCb1.Value = 0
End Sub
Public Sub checkbox1_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb1.Value = 1 Then oCb2.Value = 0
End Sub
How to determine if the radio box is selected in VBA?
Form control,
Set sht = ThisWorkbook.Sheets(1)
If sht.OptionButton1.Value = 1 Then
MsgBox "ok!"
End If
not running
Example
Option Explicit
Public Sub Example()
If ThisWorkbook.Sheets(1).OptionButtons("Option Button 1").Value > 0 Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub
I have 70 check boxes on a sheet.
I would like each Checkbox - when clicked to fill a Rectangle around that Checkbox.
If Checkbox is not clicked - the Rectangle will not be filled.
My issue is that I try to apply this code to each checkbox - but I am getting a compile error: "compile error ambiguous name detected boxcheck"
How do I prevent the compile error?
Note: Each Checkbox will have it's own unique name (1-70), and each Rectangle will have its own Unique Name (1-70). This way each Checkbox should only fill the Rectangle that the VBA IF/THEN code references. I do NOT want 1 Checkbox to fill all rectangles.
Here is my code:
Sub BoxCheck()
If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then
ActiveSheet.Shapes("Rectangle 1").Fill.ForeColor.SchemeColor = 3
End If
If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = -4146 Then
ActiveSheet.Shapes("Rectangle 1").Fill.ForeColor.SchemeColor = 1
End If
End Sub
It could be throwing you an error if you have multiple subs within a module that are named the exact same. If you copy your original code, or the one below and simply replace the # for the rectangle and box number it pertains to, it might clear up the error.
Sub BoxCheck#()
If ActiveSheet.Shapes("Check Box #").ControlFormat.Value = 1 Then
ActiveSheet.Shapes("Rectangle #").Fill.ForeColor.SchemeColor = 3
End If
If ActiveSheet.Shapes("Check Box #").ControlFormat.Value = -4146 Then
ActiveSheet.Shapes("Rectangle #").Fill.ForeColor.SchemeColor = 1
End If
End Sub
Another option would be to put each BoxCheck into a different module, but that seems excessive, especially because you have 70 of them
You can link all of your check boxes to a single sub and use Application.Caller to figure out which one called the method:
Sub BoxCheck()
Dim shp, rectName as string
Set shp = ActiveSheet.Shapes(Application.Caller)
rectName = Replace(Application.Caller, "Check Box ", "Rectangle ")
ActiveSheet.Shapes(rectName).Fill.ForeColor.SchemeColor = _
IIf(shp.ControlFormat.Value = 1, 3, 1)
End Sub
In Excel 2010, VBA, when the checkbox is unchecked, the code should not run. However it is running when the checkbox is unchecked. What am I missing?
EDIT: The unchecked checkbox results in "-4146" in the Immediate window. So I changed the IF condition to <> 1. Isn't an unchecked checkbox supposed to be equal to zero?
EDIT 2: I forgot to mention, this is a FORM CONTROL checkbox.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'delete range on the active row from column I to column M and shift up
'I=9, M = 13
Dim delAnswer As VbMsgBoxResult
Const WS_Range As String = "I:M"
'Checks to see if user wants macro turned on
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = 0 Then Exit Sub
On Error GoTo ws_exit
Application.EnableEvents = False
'Check to see if user is in applicable range for macro to run
If Not Intersect(ActiveCell, Me.Range(WS_Range)) Is Nothing Then
'Ask user to confirm running macro
With Target
aR = ActiveCell.Row
delAnswer = MsgBox("Delete I" & aR & ":M" & aR & "?", vbYesNo)
'Macro - deletes range of cells on the active row
If delAnswer = vbYes Then
Range("I" & ActiveCell.Row).Resize(, 5).Delete Shift:=xlUp
Else: GoTo ws_exit
End If
End With
End If
ws_exit:
Application.EnableEvents = True
'Debug.Print Selection.Address(ReferenceStyle:=xlA1, RowAbsolute:=False, ColumnAbsolute:=False)
'Debug.Print "ar is " & aR
End Sub
Change
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = 0
To
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = -4146
-4146 is the numerical equivalent to an unchecked Form Control check box. To test if the check box is indeed checked, use = 1 instead of = -4146
I would determine if the form checkbox is not checked in the following manner:
If Not ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then
MsgBox ("unchecked")
End If
I referred to the following answer on how to detect checked value for Form or ActiveX controls: https://stackoverflow.com/a/11991419/5692872
I found these both work by changing:
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = 0
to:
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value <> 1
or
If ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = xloff
I have a ActiveX CheckBox control on worksheet "A" and another on worksheet "B". When I check the CheckBox at "A", I want my macro to check the CheckBox at "B".
What I have tried so far:
This: Sheets("B").Shapes("CheckBox1").ControlFormat.Value = xlOn
And this: ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1
Both codes give me an error saying that the the object doesn't accept this property or method.
So it's not possible to check a CheckBox from another worksheet?
I find it useful to use a With ... End With statement to reference a worksheet as it allows multiple operations.
With Worksheets("B")
' for Form Control Checkbox
.Shapes("Check Box 2").ControlFormat.Value = xlOn
' for ActiveX Control Checkbox
.Shapes("CheckBox1").OLEFormat.Object.Object.Value = xlOn
End With
The prefix period (aka . or full stop) applies the parent worksheet.
Checkbox1 in sheet 1 to change checkbox1 in sheet 2
Private Sub CheckBox1_Click()
If Me.CheckBox1 = True Then
Sheets("Sheet2").CheckBox1.Value = True
Else
Sheets("Sheet2").CheckBox1.Value = False
End If
End Sub
For me, replacing the numbers 1 and 0 as for, respectively, the
boolean "True" and "False" did not work. Some say that ActivexControl
has "False" equal to -4146 and FormControl has "False" as equal to 0. This did not work either. The snipped below is now working.
Dim i as Integer
For i = 1 to 502
If Sheets("Sheet1").Shapes("Alpha-" & i).ControlFormat.Value = 1 Then '<-- FormControl
Sheets("Sheet2").Shapes("beta" & i).OLEFormat.Object.Object.Value = 1 '<--ActivexControl
Else
Sheets("Sheet2").Shapes("beta" & i).OLEFormat.Object.Object.Value = 0 '<--ActivexControl
End If
next i