2017-09-03 74 views
-1

我有問題使這個代碼工作 - 任何幫助非常感謝!我知道這個問題是關於第一行...我有一個Private Sub Worksheet_Change(ByVal Target As Range)上面這個代碼來解決我需要VBA代碼的其他元素。VBA代碼隱藏和取消隱藏多張紙上的空行

Private Sub HideAndUnhideRowsInOtherWorksheet() 
    For Each c In Worksheets("FlatStage").Range("A7:A32") 
     If c.Value = "" Then 
      c.EntireRow.Hidden = True 
     Else 
      c.EntireRow.Hidden = False 
     End If 
    Next 
    For Each c In Worksheets("Efficiency").Range("A7:A32") 
     If c.Value = "" Then 
      c.EntireRow.Hidden = True 
     Else 
      c.EntireRow.Hidden = False 
     End If 
    Next 
    For Each c In Worksheets("DayRate").Range("A7:A10,A14:A22,A25:A25,A28:A39") 
     If c.Value = "" Then 
      c.EntireRow.Hidden = True 
     Else 
      c.EntireRow.Hidden = False 
     End If 
    Next 
    For Each c In Worksheets("AddServ").Range ("A6:A8,A10:A11,A13:A17") 
     If c.Value = "" Then 
      c.EntireRow.Hidden = True 
     Else 
      c.EntireRow.Hidden = False 
     End If 
    Next 
    For Each c In Worksheets("Enhancement").Range("A6:A7") 
     If c.Value = "" Then 
      c.EntireRow.Hidden = True 
     Else 
      c.EntireRow.Hidden = False 
     End If 
    Next 
End Sub 
+1

是我們應該去猜測什麼問題? – jsotola

回答

0

如果從上述Worksheet_Change()調用代碼,它應該工作,但如果你從另一個模塊調用你的子,因爲它宣佈爲Private(以該特定模塊),這將是不可見的

所有你需要做的是改變PrivatePublic
(或移動到一個新的通用模塊,並使其Public有)


。另一方面,屏幕可能會被執行
而要解決這個循環在此之前關閉ScreenUpdating閃爍,之後

重新打開它,但你也可以減少代碼,以便它會更容易維護:

Option Explicit 

Public Sub HideAndUnhideRowsInOtherWorksheet() 
    Application.ScreenUpdating = False 

    ToggleRows Worksheets("FlatStage").Range("A7:A32") 
    ToggleRows Worksheets("Efficiency").Range("A7:A32") 
    ToggleRows Worksheets("DayRate").Range("A7:A10,A14:A22,A25:A25,A28:A39") 
    ToggleRows Worksheets("AddServ").Range("A6:A8,A10:A11,A13:A17") 
    ToggleRows Worksheets("Enhancement").Range("A6:A7") 

    Application.ScreenUpdating = True 
End Sub 

Private Sub ToggleRows(ByRef colRng As Range) 
    If Not colRng Is Nothing Then 
     Dim c As Range 
     For Each c In colRng 
      c.EntireRow.Hidden = Len(c.Value2) = 0 
     Next 
    End If 
End Sub 

或者更小,更快,如果你可以使用AutoFilter

Private Sub FilterRows(ByRef colRng As Range) 
    If Not colRng Is Nothing Then 
     colRng.Parent.UsedRange.Columns(colRng.Column).AutoFilter 'Filter symbol in top cell 
     colRng.AutoFilter Field:=1, Criteria1:="<>" 
    End If 
End Sub 

此子將取消隱藏所有行中的所有工作表

Public Sub UnhideAllRowsInAllWorksheets() 
    Dim ws As Worksheet 
    For Each ws In Worksheets 
     With ws.UsedRange 
      If ws.AutoFilterMode Then .AutoFilter 'ws.ShowAllData 
      .Rows.EntireRow.Hidden = False 
     End With 
    Next 
End Sub