Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over checkboxes with VBA in Excel very slow

I have an Excel Sheet with about 4500 checkboxes (I know, it sounds stupid, but it is for a customer, please do not ask...). Just wrote the VBA Sub below to uncheck all the boxes together. So far it works, but it is terribly slow, it takes more than 5 minutes until all boces are unchecked and while the Sub is running, the whole Excel Applikation grays out freezes. I know, 4500 Checkboxes is quiet a lot, but I wonder that it is really enough to bring Excel in such a trouble....Has anyone an idea?

Best Michael

Sub DeselectAll()
   Application.EnableCancelKey = False
   Application.Calculation = xlCalculationManual
   Application.ScreenUpdating = False
   Application.EnableEvents = False
   Dim wksA As Worksheet
   Dim intRow As Integer

   Set wksA = Worksheets("Companies")
   For intRow = 1 To 4513
      wksA.CheckBoxes("Checkbox_" & intRow).Value = False
   Next
 End Sub
like image 708
Michael Schwed Avatar asked Dec 04 '22 19:12

Michael Schwed


2 Answers

Just don't loop.

This is a good example of when Selection can help:

To set all checkboxes:

Sub dural()
    ActiveSheet.CheckBoxes.Select
    Selection.Value = xlOn
End Sub

To uncheck all checkboxes:

Sub dural2()
    ActiveSheet.CheckBoxes.Select
    Selection.Value = xlOf
End Sub

( tested on Forms-type checkboxes )

like image 38
Gary's Student Avatar answered Dec 13 '22 15:12

Gary's Student


Without selection:

Sub DeselectAll()
  With Worksheets("Companies").CheckBoxes
   .Value = xlOff
  End With
End Sub
like image 115
EvR Avatar answered Dec 13 '22 15:12

EvR