2016-09-21 78 views
0

我有一個循環邏輯的宏,我從另一個stackoverflow/ms支持頁面複製,但它似乎並沒有工作。VBA問題 - 通過每個工作表循環

我對VBA沒有經驗,所以我無法弄清楚爲什麼'循環遍歷所有工作表'部分不起作用。

任何人都可以看看我的代碼,並告訴我它如何修復?

Sub HideEmptyRows() 
    Dim rngName As Range 
    Dim cell As Range 
    Dim ws_count As Integer 
    Dim i As Integer 

    ws_count = ActiveWorkbook.Worksheets.Count 
    For i = 1 To ws_count 
     Application.ScreenUpdating = False 
     For Each Current In Worksheets 
      ' This code hides the adv and group merch rows 
      For Each cell In Range("eq29", "eq51") 
       If cell.Value = 0 Then 
        cell.EntireRow.Hidden = True 
        Else 
        cell.EntireRow.Hidden = False 
       End If 

      Next cell 

      ' This code hides the consulting rows 
      For Each cell In Range("eq61", "eq172") 
       If cell.Value = 0 Then 
        cell.EntireRow.Hidden = True 
        Else 
        cell.EntireRow.Hidden = False 
       End If 
      Next cell 
     Next 

    Application.ScreenUpdating = True 
    Next i 
End Sub 
+0

我想以後在投擲隨着當前每個電流線的想法,然後在每個有關的範圍之前一段時間每個單元格行和最後一個Next單元格後的End With。 除非有令人信服的理由(即測試不同或將會),爲什麼不合並範圍/單元循環? –

回答

1

按我的評論:

你還沒有指定任何範圍內的對象到父表所以它只能在活動工作表。僅僅因爲你正在循環,不會自動將表單分配給這些範圍。您需要將Current.放在所有範圍對象的前面。

外環是沒有必要的。

我重做就躲的邏輯來節省一些打字:

Sub HideEmptyRows() 

    Dim rngName As Range 
    Dim cell As Range 
    Dim current As Worksheet 


    Application.ScreenUpdating = False 
    For Each current In Worksheets 
     ' This code hides the adv and group merch rows 
     For Each cell In current.Range("EQ29:EQ51") 
      cell.EntireRow.Hidden = cell.Value = 0 
     Next cell 

     ' This code hides the consulting rows 
     For Each cell In current.Range("EQ61:EQ172") 
      cell.EntireRow.Hidden = cell.Value = 0 
     Next cell 
    Next 

    Application.ScreenUpdating = True 

End Sub