Hide/unhide rows depending on their vaule

allovera

New Member
Joined
Apr 14, 2016
Messages
11
I have this Macro to hide/unhide rows depending on the first column value.

The problem is that it examines row by row and it takes very long since the values on the columns keep changing depending on a formula.
Is there a way to examine all rows at the same time and hide/unhide them in a second. Or at the same rate that the data updates?

-----------------------------------------------------------
Sub HideRows()
BeginRow = 11
EndRow = 59
ChkCol = 3


For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False

End If
Next RowCnt
End Sub
----------------------------------------------------
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I took your short code and placed it on a sheet with very little data and no formulas, it runs fast and clean.

not sure why having formulas in your sheet would slow down the code, the range is not being deleted so you show not be having any REF# errors.
 
Last edited:
Upvote 0
It doesn’t run that slow. It takes about 5 sec for the 48 rows in the example...

But I will use the same code on another excel sheet with 1000+rows and was just wondering if there is an alternative that could examine all rows at the same time instead of 1 by 1…

Since I need it to update as fast as possible.
 
Upvote 0
I am not sure, I think the way you are doing it is pretty fast considering.

Don
 
Upvote 0
try turning off screen updating while the code runs, should speed it up slightly

Code:
Sub HideRows()


Application.ScreenUpdating = False
BeginRow = 11
EndRow = 59
ChkCol = 3


For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False


End If
Next RowCnt


Application.ScreenUpdating = True
End Sub
 
Upvote 0
Alternatively, you could add a filter to the table. Then a bit of VBA can be written to apply a filter which removes rows where the value in 'ChkCol' is blank.

Let me know if this method is of interest and I can write the code for it ;)
 
Upvote 0
You may want to try:
Code:
Application.EnableEvents = False
Application.EnableEvents = True
Put the first line before your loop and the second line after your loop.

I found out the hard way that hiding rows can trigger the calculate event, in certain instances, maybe all I don't know for sure, but depending on your sheet calculating every time a row is hidden could cause a noticeable speed difference in your code.
 
Last edited:
Upvote 0
Thx Caleeco,

That help with the annoying screen refresh while each row is evaluated. But it didn’t speed it up. I might keep it with this change. Thx!
 
Upvote 0
Thx Caleeco,

That help with the annoying screen refresh while each row is evaluated. But it didn’t speed it up. I might keep it with this change. Thx!

No problem. Given what skywriter suggested, this may help further:

Code:
With Application
        .ScreenUpdating = False: .Calculation = xlCalculationManual: .DisplayAlerts = False
 End With

BeginRow = 11
EndRow = 59
ChkCol = 3

For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "" Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False

End If
Next RowCnt

With Application
        .DisplayAlerts = True: .Calculation = xlCalculationAutomatic: .ScreenUpdating = True
End With
 
Upvote 0

Forum statistics

Threads
1,217,364
Messages
6,136,118
Members
449,993
Latest member
Sphere2215

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