Looping over checkboxes with VBA in Excel very slow
up vote
4
down vote
favorite
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
excel vba performance loops checkbox
New contributor
add a comment |
up vote
4
down vote
favorite
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
excel vba performance loops checkbox
New contributor
Does this help a bit: If wksA.CheckBoxes("Check box " & intRow).Value = xlOn Then wksA.CheckBoxes("Check box " & intRow).Value = xlOff .+Don't forget to put your events back on.
– EvR
2 days ago
No, only the checkboxes
– Michael Schwed
2 days ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
excel vba performance loops checkbox
New contributor
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
excel vba performance loops checkbox
excel vba performance loops checkbox
New contributor
New contributor
New contributor
asked 2 days ago
Michael Schwed
212
212
New contributor
New contributor
Does this help a bit: If wksA.CheckBoxes("Check box " & intRow).Value = xlOn Then wksA.CheckBoxes("Check box " & intRow).Value = xlOff .+Don't forget to put your events back on.
– EvR
2 days ago
No, only the checkboxes
– Michael Schwed
2 days ago
add a comment |
Does this help a bit: If wksA.CheckBoxes("Check box " & intRow).Value = xlOn Then wksA.CheckBoxes("Check box " & intRow).Value = xlOff .+Don't forget to put your events back on.
– EvR
2 days ago
No, only the checkboxes
– Michael Schwed
2 days ago
Does this help a bit: If wksA.CheckBoxes("Check box " & intRow).Value = xlOn Then wksA.CheckBoxes("Check box " & intRow).Value = xlOff .+Don't forget to put your events back on.
– EvR
2 days ago
Does this help a bit: If wksA.CheckBoxes("Check box " & intRow).Value = xlOn Then wksA.CheckBoxes("Check box " & intRow).Value = xlOff .+Don't forget to put your events back on.
– EvR
2 days ago
No, only the checkboxes
– Michael Schwed
2 days ago
No, only the checkboxes
– Michael Schwed
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
6
down vote
Without selection:
Sub DeselectAll()
With Worksheets("Companies").CheckBoxes
.Value = xlOff
End With
End Sub
add a comment |
up vote
5
down vote
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 )
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
it's not necessary to useSelect
, you can assign the value directly:activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
1
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
add a comment |
up vote
0
down vote
The best answer I thumbs up for is @EvR solution. I am not trying to answer but offering an idea of a workaround.
I checked the time by adding 4000 ComboBox in blank sheet in a blank workbook with a simple 3 line loop (omg I forgot to off screen updating and calculations etc). It took around 10 minutes in my old laptop. I don’t have courage to repeat the ordeal again.
When I tried to use your piece of code with looping it is taking 3-4 seconds only and with @EvR’s solution without loop and selection is taking 1-2 seconds. These times are actual time taken with Debug.Print
or writing to some cells. Actual drama unfolds after screen updates, calculations, events are enabled with the sheet active. It become highly unstable and any careless click etc cause excel to ‘not responding’ state for 2-5 mintues.
Though Customer and Boss are always right. Once in my life I succeeded to persuade someone in a similar approach of hundreds of buttons on a worksheet to something virtual. My Idea is to create virtual checkbox in the sheet. Proper cell sizing and border with validation of the cells to `=ChrW(&H2714)’ and ignore blank and a simple code like below can make it a pass-through type of work-around.
Public Prvsel As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect, Cl As Range
Set isect = Application.Intersect(Target, Range("C1:C4000"))
If isect Is Nothing Then
Set Prvsel = Nothing 'Release multiple selection
Exit Sub
End If
If isect.Cells.Count > 1 Then
Set Prvsel = isect 'storing multiple selection for next click event
Else
If Target.Value = ChrW(&H2714) Then
Target.Value = ""
Else
Target.Value = ChrW(&H2714)
End If
If Not Prvsel Is Nothing Then
For Each Cl In Prvsel.Cells
Cl.Value = Target.Value
Next Cl
End If
End If
End Sub
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Without selection:
Sub DeselectAll()
With Worksheets("Companies").CheckBoxes
.Value = xlOff
End With
End Sub
add a comment |
up vote
6
down vote
Without selection:
Sub DeselectAll()
With Worksheets("Companies").CheckBoxes
.Value = xlOff
End With
End Sub
add a comment |
up vote
6
down vote
up vote
6
down vote
Without selection:
Sub DeselectAll()
With Worksheets("Companies").CheckBoxes
.Value = xlOff
End With
End Sub
Without selection:
Sub DeselectAll()
With Worksheets("Companies").CheckBoxes
.Value = xlOff
End With
End Sub
answered 2 days ago
EvR
9791312
9791312
add a comment |
add a comment |
up vote
5
down vote
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 )
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
it's not necessary to useSelect
, you can assign the value directly:activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
1
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
add a comment |
up vote
5
down vote
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 )
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
it's not necessary to useSelect
, you can assign the value directly:activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
1
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
add a comment |
up vote
5
down vote
up vote
5
down vote
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 )
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 )
answered 2 days ago
Gary's Student
71k93460
71k93460
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
it's not necessary to useSelect
, you can assign the value directly:activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
1
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
add a comment |
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
it's not necessary to useSelect
, you can assign the value directly:activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
1
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
without selection: Sub dural() With ActiveSheet.CheckBoxes .Value = xlOff End With End Sub Nice GS
– EvR
2 days ago
it's not necessary to use
Select
, you can assign the value directly: activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
it's not necessary to use
Select
, you can assign the value directly: activesheet.checkboxes.value = xlOff
– FunThomas
2 days ago
1
1
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@EvR thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
@FunThomas thanks....because the topic of grouping Objects is interesting, consider Posting you idea.
– Gary's Student
2 days ago
add a comment |
up vote
0
down vote
The best answer I thumbs up for is @EvR solution. I am not trying to answer but offering an idea of a workaround.
I checked the time by adding 4000 ComboBox in blank sheet in a blank workbook with a simple 3 line loop (omg I forgot to off screen updating and calculations etc). It took around 10 minutes in my old laptop. I don’t have courage to repeat the ordeal again.
When I tried to use your piece of code with looping it is taking 3-4 seconds only and with @EvR’s solution without loop and selection is taking 1-2 seconds. These times are actual time taken with Debug.Print
or writing to some cells. Actual drama unfolds after screen updates, calculations, events are enabled with the sheet active. It become highly unstable and any careless click etc cause excel to ‘not responding’ state for 2-5 mintues.
Though Customer and Boss are always right. Once in my life I succeeded to persuade someone in a similar approach of hundreds of buttons on a worksheet to something virtual. My Idea is to create virtual checkbox in the sheet. Proper cell sizing and border with validation of the cells to `=ChrW(&H2714)’ and ignore blank and a simple code like below can make it a pass-through type of work-around.
Public Prvsel As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect, Cl As Range
Set isect = Application.Intersect(Target, Range("C1:C4000"))
If isect Is Nothing Then
Set Prvsel = Nothing 'Release multiple selection
Exit Sub
End If
If isect.Cells.Count > 1 Then
Set Prvsel = isect 'storing multiple selection for next click event
Else
If Target.Value = ChrW(&H2714) Then
Target.Value = ""
Else
Target.Value = ChrW(&H2714)
End If
If Not Prvsel Is Nothing Then
For Each Cl In Prvsel.Cells
Cl.Value = Target.Value
Next Cl
End If
End If
End Sub
add a comment |
up vote
0
down vote
The best answer I thumbs up for is @EvR solution. I am not trying to answer but offering an idea of a workaround.
I checked the time by adding 4000 ComboBox in blank sheet in a blank workbook with a simple 3 line loop (omg I forgot to off screen updating and calculations etc). It took around 10 minutes in my old laptop. I don’t have courage to repeat the ordeal again.
When I tried to use your piece of code with looping it is taking 3-4 seconds only and with @EvR’s solution without loop and selection is taking 1-2 seconds. These times are actual time taken with Debug.Print
or writing to some cells. Actual drama unfolds after screen updates, calculations, events are enabled with the sheet active. It become highly unstable and any careless click etc cause excel to ‘not responding’ state for 2-5 mintues.
Though Customer and Boss are always right. Once in my life I succeeded to persuade someone in a similar approach of hundreds of buttons on a worksheet to something virtual. My Idea is to create virtual checkbox in the sheet. Proper cell sizing and border with validation of the cells to `=ChrW(&H2714)’ and ignore blank and a simple code like below can make it a pass-through type of work-around.
Public Prvsel As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect, Cl As Range
Set isect = Application.Intersect(Target, Range("C1:C4000"))
If isect Is Nothing Then
Set Prvsel = Nothing 'Release multiple selection
Exit Sub
End If
If isect.Cells.Count > 1 Then
Set Prvsel = isect 'storing multiple selection for next click event
Else
If Target.Value = ChrW(&H2714) Then
Target.Value = ""
Else
Target.Value = ChrW(&H2714)
End If
If Not Prvsel Is Nothing Then
For Each Cl In Prvsel.Cells
Cl.Value = Target.Value
Next Cl
End If
End If
End Sub
add a comment |
up vote
0
down vote
up vote
0
down vote
The best answer I thumbs up for is @EvR solution. I am not trying to answer but offering an idea of a workaround.
I checked the time by adding 4000 ComboBox in blank sheet in a blank workbook with a simple 3 line loop (omg I forgot to off screen updating and calculations etc). It took around 10 minutes in my old laptop. I don’t have courage to repeat the ordeal again.
When I tried to use your piece of code with looping it is taking 3-4 seconds only and with @EvR’s solution without loop and selection is taking 1-2 seconds. These times are actual time taken with Debug.Print
or writing to some cells. Actual drama unfolds after screen updates, calculations, events are enabled with the sheet active. It become highly unstable and any careless click etc cause excel to ‘not responding’ state for 2-5 mintues.
Though Customer and Boss are always right. Once in my life I succeeded to persuade someone in a similar approach of hundreds of buttons on a worksheet to something virtual. My Idea is to create virtual checkbox in the sheet. Proper cell sizing and border with validation of the cells to `=ChrW(&H2714)’ and ignore blank and a simple code like below can make it a pass-through type of work-around.
Public Prvsel As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect, Cl As Range
Set isect = Application.Intersect(Target, Range("C1:C4000"))
If isect Is Nothing Then
Set Prvsel = Nothing 'Release multiple selection
Exit Sub
End If
If isect.Cells.Count > 1 Then
Set Prvsel = isect 'storing multiple selection for next click event
Else
If Target.Value = ChrW(&H2714) Then
Target.Value = ""
Else
Target.Value = ChrW(&H2714)
End If
If Not Prvsel Is Nothing Then
For Each Cl In Prvsel.Cells
Cl.Value = Target.Value
Next Cl
End If
End If
End Sub
The best answer I thumbs up for is @EvR solution. I am not trying to answer but offering an idea of a workaround.
I checked the time by adding 4000 ComboBox in blank sheet in a blank workbook with a simple 3 line loop (omg I forgot to off screen updating and calculations etc). It took around 10 minutes in my old laptop. I don’t have courage to repeat the ordeal again.
When I tried to use your piece of code with looping it is taking 3-4 seconds only and with @EvR’s solution without loop and selection is taking 1-2 seconds. These times are actual time taken with Debug.Print
or writing to some cells. Actual drama unfolds after screen updates, calculations, events are enabled with the sheet active. It become highly unstable and any careless click etc cause excel to ‘not responding’ state for 2-5 mintues.
Though Customer and Boss are always right. Once in my life I succeeded to persuade someone in a similar approach of hundreds of buttons on a worksheet to something virtual. My Idea is to create virtual checkbox in the sheet. Proper cell sizing and border with validation of the cells to `=ChrW(&H2714)’ and ignore blank and a simple code like below can make it a pass-through type of work-around.
Public Prvsel As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect, Cl As Range
Set isect = Application.Intersect(Target, Range("C1:C4000"))
If isect Is Nothing Then
Set Prvsel = Nothing 'Release multiple selection
Exit Sub
End If
If isect.Cells.Count > 1 Then
Set Prvsel = isect 'storing multiple selection for next click event
Else
If Target.Value = ChrW(&H2714) Then
Target.Value = ""
Else
Target.Value = ChrW(&H2714)
End If
If Not Prvsel Is Nothing Then
For Each Cl In Prvsel.Cells
Cl.Value = Target.Value
Next Cl
End If
End If
End Sub
answered yesterday
Ahmed AU
36018
36018
add a comment |
add a comment |
Michael Schwed is a new contributor. Be nice, and check out our Code of Conduct.
Michael Schwed is a new contributor. Be nice, and check out our Code of Conduct.
Michael Schwed is a new contributor. Be nice, and check out our Code of Conduct.
Michael Schwed is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225157%2flooping-over-checkboxes-with-vba-in-excel-very-slow%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Does this help a bit: If wksA.CheckBoxes("Check box " & intRow).Value = xlOn Then wksA.CheckBoxes("Check box " & intRow).Value = xlOff .+Don't forget to put your events back on.
– EvR
2 days ago
No, only the checkboxes
– Michael Schwed
2 days ago