Macro to Hide Zero Values

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
184
Office Version
  1. 365
Platform
  1. Windows
I am using a working macro to hide all rows that column B is < 0.
The issue is that it runs slow, is there a more efficient or faster way of accomplishing this?
Here is my working macro:

VBA Code:
Sub HideZeros()
Dim lrA As Long
StartRow = 10
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
iCol = 2
For i = StartRow To LastRow
If Cells(i, iCol).Value > 0 Then
Cells(i, iCol).EntireRow.Hidden = False
Else
Cells(i, iCol).EntireRow.Hidden = True
End If
Next i
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Is this any faster
VBA Code:
Sub HideZeros()
Application.ScreenUpdating = False
Dim LastRow As Long
StartRow = 10
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
iCol = 2
For i = LastRow To StartRow Step -1
If Cells(i, iCol).Value > 0 Then
Cells(i, iCol).EntireRow.Hidden = False
Else
Cells(i, iCol).EntireRow.Hidden = True
End If
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Is this any faster
VBA Code:
Sub HideZeros()
Application.ScreenUpdating = False
Dim LastRow As Long
StartRow = 10
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
iCol = 2
For i = LastRow To StartRow Step -1
If Cells(i, iCol).Value > 0 Then
Cells(i, iCol).EntireRow.Hidden = False
Else
Cells(i, iCol).EntireRow.Hidden = True
End If
Next i
Application.ScreenUpdating = True
End Sub
Speed is the same
 
Upvote 0
You can try using autofilter. I use that all the time to filter my ranges.

Here is an example:
Code:
Sub HideZeros()
Dim Filter_rng as Range
Dim iCol as Long
Dim StartRow as Long

StartRow = 10
iCol = 2
Filter_rng = Range(Cells(StartRow - 1, iCol), Cells(Rows.Count, iCol).End(xlUp)
Filter_rng.autofilter Field:=1, Criteria1:=">0"

End Sub
 
Upvote 0
You can try using autofilter. I use that all the time to filter my ranges.

Here is an example:
Code:
Sub HideZeros()
Dim Filter_rng as Range
Dim iCol as Long
Dim StartRow as Long

StartRow = 10
iCol = 2
Filter_rng = Range(Cells(StartRow - 1, iCol), Cells(Rows.Count, iCol).End(xlUp)
Filter_rng.autofilter Field:=1, Criteria1:=">0"

End Sub
This code is giving me an error, I think something is missing:
1699400805598.png
 
Upvote 0
Oh wait, I forgot to add a Set
VBA Code:
Sub HideZeros()
Dim Filter_rng as Range
Dim iCol as Long
Dim StartRow as Long

StartRow = 10
iCol = 2
Set Filter_rng = Range(Cells(StartRow - 1, iCol), Cells(Rows.Count, iCol).End(xlUp)
Filter_rng.autofilter Field:=1, Criteria1:=">0"

End Sub
 
Upvote 0
Solution
Oh wait, I forgot to add a Set
VBA Code:
Sub HideZeros()
Dim Filter_rng as Range
Dim iCol as Long
Dim StartRow as Long

StartRow = 10
iCol = 2
Set Filter_rng = Range(Cells(StartRow - 1, iCol), Cells(Rows.Count, iCol).End(xlUp)
Filter_rng.autofilter Field:=1, Criteria1:=">0"

End Sub
Still getting an error

1699446882079.png
 
Upvote 0
Do you have headings in row 9?
 
Upvote 0
Still getting an error
That line is missing a closing bracket and s/be
VBA Code:
Set Filter_rng = Range(Cells(StartRow - 1, iCol), Cells(Rows.Count, iCol).End(xlUp))

I have not looked at whether it is faster though. You might want to respond to Fluff and I am sure he will be able will be able to provide faster option.
 
Upvote 0
That line is missing a closing bracket and s/be
VBA Code:
Set Filter_rng = Range(Cells(StartRow - 1, iCol), Cells(Rows.Count, iCol).End(xlUp))

I have not looked at whether it is faster though. You might want to respond to Fluff and I am sure he will be able will be able to provide faster option.
It works now, adding the missing bracket and changing the filed ot number 2(my fault) and yes it is much faster, thanks all. I will mark mushypeas as the answer :-)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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