zoom38
10-01-2015, 08:20 AM
Hello all, I have a sub that goes thru a group of six checkboxes so that when a checkbox is clicked/checked in that group, the others will be unchecked. I actually have 13 groups of 6 checkboxes to do this for (checkboxes 5 - 82) and am looking to simplify the code so that its not ridiculously long. I thought a loop would do but I can't figure it out. I pondered an array but don't really know my way with them. Is there a way to shorten my code below to accomplish this?
Application.EnableEvents = False
ActiveSheet.Unprotect
'Checkboxes 5-10
If z = 5 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 4).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 5).Object.Value = 0
GoTo a
ElseIf z = 6 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 4).Object.Value = 0
GoTo a
ElseIf z = 7 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 3).Object.Value = 0
GoTo a
ElseIf z = 8 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
GoTo a
ElseIf z = 9 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
GoTo a
ElseIf z = 10 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
GoTo a
End If
a:
ActiveSheet.Unprotect
Application.EnableEvents = True
Application.EnableEvents = False
ActiveSheet.Unprotect
'Checkboxes 5-10
If z = 5 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 4).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 5).Object.Value = 0
GoTo a
ElseIf z = 6 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 4).Object.Value = 0
GoTo a
ElseIf z = 7 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 3).Object.Value = 0
GoTo a
ElseIf z = 8 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
GoTo a
ElseIf z = 9 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
GoTo a
ElseIf z = 10 And ActiveSheet.OLEObjects("CheckBox" & z).Object.Value = True Then
ActiveSheet.OLEObjects("CheckBox" & z - 3).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 2).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z - 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 1).Object.Value = 0
ActiveSheet.OLEObjects("CheckBox" & z + 2).Object.Value = 0
GoTo a
End If
a:
ActiveSheet.Unprotect
Application.EnableEvents = True