I'm trying to check the value of these simple radio button groups but my syntax is off, does anyone know what to change?
Note: they are Excel Option Buttons not ActiveX ones and they are not on a userform.
If Worksheets("Input").Shapes("Option Button 3").Select.Value = xlOn Then
MsgBox "fir"
ElseIf Worksheets("Input").Shapes("Option Button 4").Select.Value = xlOn Then
MsgBox "sec"
Else
MsgBox "none" 'in case they were deleted off the sheet
End If
Try this
Sub ZX()
Dim shp3 As Shape
Dim shp4 As Shape
On Error Resume Next
Set shp3 = Worksheets("Input").Shapes("Option Button 3")
Set shp4 = Worksheets("Input").Shapes("Option Button 4")
On Error Goto 0
If shp3 Is Nothing Then
If shp4 Is Nothing Then
MsgBox "none" 'in case they were deleted off the sheet
ElseIf shp4.ControlFormat.Value = xlOn Then
MsgBox "sec"
Else
MsgBox "Only Button 4 exists and it is off"
End If
Else
If shp3.ControlFormat.Value = xlOn Then
MsgBox "fir"
Else
If shp4 Is Nothing Then
MsgBox "Only Button 3 exists and it is off"
ElseIf shp4.ControlFormat.Value = xlOn Then
MsgBox "sec"
Else
MsgBox "Both exists, both are off"
End If
End If
End If
End Sub
I had a similar problem. To solve it, I decided to use a toggle button with this macro VBA code to access its value and toggle my display accordingly to it:
Call ThisWorkbook.Sheets("MySheet").toggleDisplay(CBool(ThisWorkbook.Sheets("MySheet").ToggleButton1.Value))
Related
This should be simple. I am working with userforms. I want to save a number that is stored in a cell and concatenate it with "lbl" and store it in a variable called Label. This works. I Then need to use the property .Caption but get a 424 error. Replacing the variable with what is stored in it ("lbl1"), the code runs.
Not sure why this is not working. Any help appreciated.
Sub ErrorExample()
Dim Label As String
'Clear previous click
If Worksheets("BackEnd").Range("D2").Value = "" Then
'No previous click
Else
'This does not work
Label = "lbl" & Worksheets("BackEnd").Range("D2").Value
With Label
'Clear previous click
.Caption = ""
End With
'This works
With lbl2
'Clear previous click
.Caption = ""
End With
End If
End Sub
Userform: Reference a Control By Its Name
Copy the code into the user form's module.
Private Sub ToClearOrNotToClear()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("BackEnd")
Dim Label As String: Label = CStr(ws.Range("D2").Value)
Dim lbl As Control
If Len(Label) > 0 Then
On Error Resume Next
Set lbl = Controls("lbl" & Label)
On Error GoTo 0
If lbl Is Nothing Then
MsgBox "Label not found!", vbCritical
Exit Sub
End If
If Len(CStr(lbl.Caption)) = 0 Then
MsgBox "Label is clear!", vbExclamation
Exit Sub
End If
lbl.Caption = ""
MsgBox "Label cleared.", vbInformation
End If
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 the following code to uncheck all the Checkboxes in sheet.
It doesn't change the boxes. I get the message box confirming that the range is green and therefore the loop should kick in.
Sub Changeboxes()
'Dim cb As CheckBox'
If Sheet1.Range("a1").Value = "green" Then
MsgBox "The range is green.", vbOKOnly
For Each cb In Sheet1.CheckBoxes
cb.Value = True
Next cb
Else
MsgBox "The range is NOT green.", vbOKOnly
End If
MsgBox "Checked availability updated.", vbOKOnly
End Sub
Change this line : cb.Value = True to cb.Value = False to uncheck the boxes.
EDIT
Yes, if the checkboxes are activex objects then the above code will not work. Instead, use the following code:
Sub ChangeBoxes()
Dim obj As OLEObject
If Sheet1.Range("a1").Value = "green" Then
MsgBox "The range is green.", vbOKOnly
For Each obj In Sheet1.OLEObjects
obj.Object.Value = True
Next
MsgBox "Checked availability updated.", vbOKOnly
Else
MsgBox "The range is NOT green.", vbOKOnly
End If
End Sub
I have also moved the last messagebox into the first part of the if statement because it was popping up whether or not the value in A1 was green.
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'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