A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Thankyou OssieMac, that worked, its really useful
Dan
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have the following code in a Macro in an excel spreadsheet
"Sub CheckBox2()
Dim xCheckBox As CheckBox
For Each xCheckBox In Application.ActiveSheet.CheckBoxes
If xCheckBox.Name <> Application.ActiveSheet.CheckBoxes("Check Box 2").Name Then
xCheckBox.Value = Application.ActiveSheet.CheckBoxes("Check Box 2").Value = False
End If
Next
End Sub"
There are 4 of these with check boxes and allows me to check one box and turn others off?
I want to create the same again but not to interfere with each other and they do????
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Thankyou OssieMac, that worked, its really useful
Dan
Am I interpreting correctly that you want to have multiple groups of CheckBoxes and in each group when a box is checked you want to uncheck all other boxes in the same group without affecting the Checkboxes in other groups? If so then the following procedure and code.
You need to Group each set of CheckBoxes as follows.
Copy all of the code below into a standard module.
Feel free to get back to me if any problems or questions relating to this.
Sub AssignMacro()
'This sub will assign macro "UpdateChkBoxes" to all the CheckBoxes in each Group
Dim shp As Shape
Dim shpChk As Shape
For Each shp In ActiveSheet.Shapes
If shp.Type = msoGroup Then 'Ignore if not a Group
For Each shpChk In shp.GroupItems 'Loop through shapes in Group
If shpChk.FormControlType = xlCheckBox Then 'If CheckBox
shpChk.OnAction = "UpdateChkBoxes" 'Set macro to be called when CheckBox changes
shpChk.ControlFormat.Value = xlOff 'Initialize value of CheckBox to False (xlOff is False)
End If
Next shpChk
End If
Next shp
End Sub
Sub UpdateChkBoxes()
Dim shpChk As Shape
Dim chkBox As Shape
Dim strGrpName As String
Set chkBox = ActiveSheet.Shapes(Application.Caller) 'The CheckBox that called this sub
strGrpName = chkBox.ParentGroup.Name
'Next line: 'Code only runs if box is checked. If unchecked code does not process
If chkBox.ControlFormat.Value = xlOn Then 'If checkBox checked
'Next line: Loop through all of the CheckBoxes in the Group
For Each shpChk In ActiveSheet.Shapes(strGrpName).GroupItems
If shpChk.FormControlType = xlCheckBox Then 'Don't change any shape that is NOT a CheckBox
If shpChk.Name <> chkBox.Name Then 'Don't change the Checked Box
shpChk.ControlFormat.Value = xlOff 'xlOff is False value
End If
End If
Next
End If
End Sub
If you use Optionbuttons instead you do not need any line of code. ;-)
Anyway, assign the macro below to each Checkbox.
Andreas.
Sub MyCheckBoxes_Click()
Dim CB As CheckBox, ThisCB As CheckBox
'Refer to the checkbox that called us
Set ThisCB = ActiveSheet.CheckBoxes(Application.Caller)
'Visit all CheckBoxes
For Each CB In ActiveSheet.CheckBoxes
'Turn all others off
If CB.Name <> ThisCB.Name Then CB.Value = False
Next
End Sub