title: How can I improve performance in an Excel VBA For loop with nested If loop?
body: <p>This is my first post, so if I need to make any changes, please let me know.</p>
  
  <p>I've found a few examples of ways to optimize loops, but I cannot seem to apply any of them effectively to my code.  What I'm trying to do is loop through about 170 cells in a single column, and hide or show the entire row based on whether the cell value is 0 or not.</p>
  
  <p>I want the code to run each time I activate certain sheets.  Right now this piece of code is taking about 4 seconds to run.  It seems like it should be much faster than that! That's why I'm here for help.</p>
  
  <p>Here is the code I'm using (FormatSheet returns a BOOLEAN where True means that it is okay to perform this code on this sheet and False means to skip performing this code on this sheet:</p>
  
  <pre><code>Private Sub mobjWb_SheetActivate(ByVal Sh As Object)
  
  Dim r As Long
  Dim z As Long
  Dim varray As Variant
  
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = False
  Application.DisplayStatusBar = False
  Application.EnableEvents = False
  ActiveSheet.DisplayPageBreaks = False
  
  If Not FormatSheet(Sh) Then
  Exit Sub
  End If
  
  Set varray = Range("$F$1", Cells(Rows.count, "F").End(x1up)).Value
  
  
  For Each r In varray
  
  z = r.Value
  If z = 0 Then
  Range("F" &amp; r).EntireRow.Hidden = True
      Else
      Range("F" &amp; r).EntireRow.Hidden = False
  End If
  Next r
  
  
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
  Application.DisplayStatusBar = True
  Application.EnableEvents = True
  ActiveSheet.DisplayPageBreaks = True
  
  
  End Sub
  </code></pre>