Hide multiple rows across scheets

时间:2019-01-18 18:15:43

标签: excel vba hide rows

This code hides lines on the Datasheet Tab, whatever rows it hides on the datasheet tab I want it to hide on my other 3 tabs, so that they will all look uniform, but I am having a hard time getting it to work.

     Private Sub DeleteRows_Click()
        Dim FinalCell as String

        Sheets("Datasheet").Activate
        Sheets("Datasheet").Select
        FinalRow = 0
        y = 0
        Z = 7                               'Column to check for blanks (7 = "G")
        Z1 = 1                              'Last Valid Row
        Z2 = 1                              'Used used to determine how many columns to look for data in last row

        'Find Final Row with data Z2 represents how many columns to look for any data at all
        '300 = "KN"

        For x = 1 To Z2
            y = Cells(Rows.Count, x).End(xlUp).Row
            If FinalRow < y Then FinalRow = y
        Next x

       'Find Final Row with data Z represents Final Row with data in specified column

        For x = Z To Z
            y = Cells(Rows.Count, x).End(xlUp).Row
            If FinalRow < y Then FinalRow = y
        Next x

        Z1 = y 'Reset Z1 to stop deleting at last data in specified column

        'Look for blanks in column Z
        For x = FinalRow To Z1 Step -1       'Loop counts up from last row
            CellGot = Worksheets("Datasheet").Cells(x, Z)

            'Empty and Null are both possibilities for blanks
            If Worksheets("Datasheet").Cells(x, Z) = "" Or Worksheets("Datasheet").Cells(x, Z) = "Empty" Then
                FinalCell = x & ":" & x
                Rows(FinalCell).Hidden = True      'Deletes the row
            End If
        Next x


End Sub

1 个答案:

答案 0 :(得分:2)

If those are the only worksheets in your workbook then add Dim w as Worksheet at the top of your sub and change

    If Worksheets("Datasheet").Cells(x, Z) = "" Or Worksheets("Datasheet").Cells(x, Z) = "Empty" Then
        FinalCell = x & ":" & x
        Rows(FinalCell).Hidden = True      'Deletes the row
    End If

To

    If Worksheets("Datasheet").Cells(x, Z) = "" Or Worksheets("Datasheet").Cells(x, Z) = "Empty" Then
        FinalCell = x & ":" & x
        For Each w In ThisWorkbook.Worksheets
            w.Rows(FinalCell).Hidden = True
        Next w
    End If

If they aren't the only worksheets in your workbook then

    If Worksheets("Datasheet").Cells(x, Z) = "" Or Worksheets("Datasheet").Cells(x, Z) = "Empty" Then
        FinalCell = x & ":" & x
        Worksheets("DataSheet").Rows(FinalCell).Hidden = True
        Worksheets("YourSheet1").Rows(FinalCell).Hidden = True
        Worksheets("YourSheet2").Rows(FinalCell).Hidden = True
        Worksheets("YourSheet3").Rows(FinalCell).Hidden = True
    End If