Macro to Hide Zero Values

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
174
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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