Macro to hide rows running slowly

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
148
Office Version
  1. 365
Platform
  1. Windows
I have a very simple macro which hides an entire row if the cell values in column AS (rows 1 through 140) are "Yes". The code I am using is below. The macro does the job but takes 5-6 seconds to hide the necessary rows. Given the fact the range is very small I am thinking there might be a better way to write this so it runs a little faster. Does anyone have a suggestion? Thanks in advance.

Sub Hide_Rows_Containing_Value()

Dim c As Range

For Each c In Range("AS1:AS140").Cells
If c.Value = "Yes" Then
c.EntireRow.Hidden = True

End If
Next c

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this.
VBA Code:
Sub Hide_Rows_Containing_Value()
Dim rngToHide As Range
Dim c As Range

    For Each c In Range("AS1:AS140").Cells
        If c.Value = "Yes" Then
            If rngToHide Is Nothing Then
                Set rngToHide = c.EntireRow
            Else
                Set rngToHide = Union(rngToHide, c.EntireRow)
            End If
        End If
    Next c

    If Not rngToHide Is Nothing Then
        rngToHide.Hidden = True
    End If
End Sub
 
Upvote 0
Try using the Autofilter with does not equal yes as the criteria.

VBA Code:
Range("AS1:AS140").Autofilter 1, "<>Yes"

Untested code as in my phone.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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