VBA code runs slow with check box

Lisa116

New Member
Joined
Jul 19, 2019
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Is there a better code for hiding rows based on 1 criteria when the check box is marked? Below is what I have and it takes close to a minute to cycle through:
Sub CheckBox1_Click()
'"Hide unused plans" on Medical FI sheet
If Range("S1").Value = "True" Then
Call Hide
ElseIf Range("S1").Value = "False" Then
Call Unhide
Else
Return
End If
End Sub

Sub Hide()
'Hides unused plans on Medical FI sheet
Application.ScreenUpdating = False
Application.Calculation = xlManual
Dim c As Range
For Each c In Range("N29:N210,N220:N375,N539:N720,N923:N1104,N1114:N1295")
If c.Value = "" Then Rows(c.Row).Hidden = True
Next
Range("A25").Select
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
End Sub

Thanks, Lisa116
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try changing the following lines
Rich (BB code):
Dim c As Range
For Each c In Range("N29:N210,N220:N375,N539:N720,N923:N1104,N1114:N1295")
If c.Value = "" Then Rows(c.Row).Hidden = True
Next
to
VBA Code:
Dim c As Range, rToHide As Range
For Each c In Range("N29:N210,N220:N375,N539:N720,N923:N1104,N1114:N1295")
    Debug.Print c.Address, c.Value
    If c.Value = "" Then
        If rToHide Is Nothing Then
            Set rToHide = c
        Else
            Set rToHide = Application.Union(rToHide, c)
        End If
    End If
Next
rToHide.EntireRow.Hidden = True

Bye
 
Upvote 0
If you always have a value in cell N28, you could use the following:
Rich (BB code):
Sub Hide()
  On Error Resume Next
  Range("N28:N210,N220:N375,N539:N720,N923:N1104,N1114:N1295").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
End Sub
 
Upvote 0
You can actually start at N29, try this:

VBA Code:
Sub Hide()
  On Error Resume Next
  Range("N29:N210,N220:N375,N539:N720,N923:N1104,N1114:N1295").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

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