VBA问题 – 遍历每个工作表

我有一个循环逻辑的macros,我从另一个stackoverflow / ms支持页面复制,但它似乎并没有工作。

我没有经验的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 

根据我的评论:

您尚未将任何范围对象分配给父工作表,因此它只能在活动工作表上工作。 只是因为你循环不会自动分配表单到这些范围。 你将需要把Current. 在所有范围对象的前面。

外环是没有必要的。

我redid隐藏的逻辑来保存一些input:

 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