• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Faster code than looping code

Cammandk

Member
I have this code that does the job but can be slow as it's looping. Goes through 4000 lines.
Is there a faster solution - sure there is?

Code:
Sub HideBudgetRows()
Dim cel As Range
Dim rng As Range
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual

  Set rng = Sheets("Budget").Range("A10", Range("EndOfBudgetRange"))
  For Each cel In rng
  If cel.Value = 0 Then
  cel.EntireRow.Hidden = True
 
  End If
 
  Next cel
 
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic
 
End Sub
 
I don't think so

But will be interesting to see the responses

You can simplify the If statement to one line

Replace
Code:
  If cel.Value = 0 Then
  cel.EntireRow.Hidden = True
 
  End If
with:
Code:
  If cel.Value = 0 Then cel.EntireRow.Hidden = True
 
Have you tried Filtering?

Code:
Sub HideBudgetRows()
Dim cel As Range
Dim rng As Range
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual

  Set rng = Sheets("Budget").Range("A10", Range("EndOfBudgetRange"))

  Rng.AutoFilter Field:=1, Criteria1:"<>0"

  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic

End Sub

You may have to adjust the code slightly to suit your range/data fields
Post your file if you want a hand
 
Thanks for this I'll try the filtering.
Have seen some code around that seems to select all the lines with "0" and then just deletes 1 time.
As you say I see what else might come back.
DK
 
I have lil doubt on the name range.. :)
May be it was dynamic and in each hidden in re-calculates..(you are hidding a single row "X" times, if the row contain "X" number of 0 (zero)

Can you give this a try..

Code:
Sub HideBudgetRows()
Dim cel As Range
Dim rng As Range
  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual

  Set rng = Sheets("Budget").Range(Range("A10"), Range("EndOfBudgetRange"))
  For i = 1 To rng.Rows.Count
  If Application.CountIf(rng.Rows(i), "0") Then
  rng.Rows(i).EntireRow.Hidden = True
  End If
  Next i
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic
End Sub
 
Last edited:
Hi, Cammandk!
Hui yet gave you the simplest and fastest solution, Autofilter. Tried it yet?
Regards!
 
Back
Top