Checking checkboxes in a range

Peltz

Board Regular
Joined
Aug 30, 2011
Messages
87
Hi there
I manage tocheck or uncheck all checkboxes in my sheet. However, what I need to do is checking and unchecking all checkboxes in a range, eg B5:B17. Any help would be appreciated.

Thanks
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Change line below:-
Code:
  c.Value = IIf(c = xlOn, xlOff, xlOn) '

To this line:-
Code:
c.Value = xlOff
 
Upvote 0
Works Perfect! Tank you so much. Saves me alot of work!


One more thing

Could you explin what the 'IIf' and -Checkbox.left = 70' command/part means. Thank you.
 
Upvote 0
You're welcome
The "iif" statement is just to alter the Checkbox from the state its in "Checked/Unchecked" to the opposite State
and the "Checkbox1.left" was me just me tidying the checkboxes so they where all the same distance from the left hand side of the sheet. ( You can remove that Line).
 
Upvote 0
Great.

But, still have a problem:

These are now the codes im using to controll the checkboxes. I set the range A1:A10 for simplicity.
Code:
Sub Test()
Dim myCBX As CheckBox
Dim wks As Worksheet
Dim rngCB As Range
Dim strCap As String
Dim lCbx As Long
Dim i As Integer
Set wks = ActiveSheet
'''change this range if required
Set rngCB = wks.Range("A1:A10")
'''add caption text, if required
'strCap = "Test"
'lCbx = 1

For Each c In rngCB
  With c
    Set myCBX = wks.CheckBoxes.Add _
      (Top:=.Top, Width:=.Width, _
       Height:=.Height, Left:=.Left)
  End With
  With myCBX
    .Display3DShading = True
    .Name = "cb"
'''change linked cell offset, if required
''' Offet(rows, columns)
    .LinkedCell = c.Offset(0, 1) _
        .Address(external:=True)
    .Caption = "" 'strCap & lCbx
'''add name of macro to run, if required
'    .OnAction = ThisWorkbook.Name _
'        & "!mycbxMacro"
  'lCbx = lCbx + 1
  End With
Next c
End Sub

Code:
Private Sub CommandButton3_Click()
Dim c As CheckBox
Dim Ws As Worksheet
Set Ws = ActiveSheet
For Each c In Ws.CheckBoxes
 'c.Left = 70
 If Not Intersect(c.TopLeftCell, Range("A1:A10")) Is Nothing Then
  c.Value = xlOff
  End If
Next c
End Sub

For some reason your code does not disable the checkboxes.
 
Last edited:
Upvote 0
In your first code you have Given each checkbox the same name, because of that when you loop through them (second code) the loop thinks they are all in cell "A1".

I have added a variable "n" to the first code as below and it now seems to work.
Code:
  .Display3DShading = True
    n = n + 1
    .Name = "cb" & n
 
Upvote 0
In your first code you have Given each checkbox the same name, because of that when you loop through them (second code) the loop thinks they are all in cell "A1".

I have added a variable "n" to the first code as below and it now seems to work.
Code:
  .Display3DShading = True
    n = n + 1
    .Name = "cb" & n


Works like a charm!

Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top