Alternative to For Loop for Hiding Rows

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Using a for loop to hide a section of rows, but this workbook I'm using is bound to Excel 2003 and the booklet is very large, which causes huge delays.

Currently, using the following, but it takes a long time and can cause the booklet to crash. Is there a way to make it so the rows are all selected at once and hidden?

VBA Code:
dim i as integer

For i = 300 to 350
if cells(i,1).value = 0 then
cells(i,1).entirerow.hidden = true
else
cells(i,1).entirerow.hidden = false
end if
next i
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You could use a Filter on that section of rows to hide all rows equal to 0, i.e.
VBA Code:
    Range("A300:A350").AutoFilter
    ActiveSheet.Range("A300:A350").AutoFilter Field:=1, Criteria1:="<>0"
 
Upvote 0
Solution
You could use a Filter on that section of rows to hide all rows equal to 0, i.e.
VBA Code:
    Range("A300:A350").AutoFilter
    ActiveSheet.Range("A300:A350").AutoFilter Field:=1, Criteria1:="<>0"
Thank you! that seems to do the trick.

Had to change the Criterial:="<>0" to Criterial:="1".
 
Upvote 0
If you need to include row 300 in the hide/don't hide you need to add a line for that as Autofilter assumes this is a header row and won't apply the filter to it.

VBA Code:
    With Range("A300:A350")
        .AutoFilter Field:=1, Criteria1:="<>0", VisibleDropDown:=False
        If .Cells(1) = 0 Then .Cells(1).EntireRow.Hidden = True Else .Cells(1).EntireRow.Hidden = False
    End With
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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