更改后自动隐藏空白行

时间:2015-07-28 12:12:57

标签: excel-vba hide rows vba excel

在我的工作表(book1)中查看来自另一个工作表(book2)的数据,我需要自动隐藏所有空白行(即使它们包含公式),如果值自动更改,则取消隐藏它们。 意思是如果我改变了第2册中的值,那么第1册书应该通过隐藏或取消隐藏行来自动作出反应。

这是代码。

Sub hideEmptyRows()
    Application.ScreenUpdating = False
    For i = 1 To 117
        If ActiveSheet.Cells(i, 1) = "" Then 
            ActiveSheet.Cells(i, 1).EntireRow.Hidden = True
        End If 
    Next i Application.ScreenUpdating = True
 End Sub

1 个答案:

答案 0 :(得分:0)

Sub hideEmptyRows()
    Application.ScreenUpdating = False
    ActiveSheet.Cells.Rows.Hidden = False 'unhide all
    LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'find the last row
    For i = 1 To LastRow
        If ActiveSheet.Cells(i, 1).Value2 = "" Or ActiveSheet.Cells(i, 1).Value2 = 0 Or IsError(ActiveSheet.Cells(i, 1).Value2) Then 'if it's an empty string, a 0 (empty cell) or an error
            ActiveSheet.Cells(i, 1).EntireRow.Hidden = True 'then hide it
        End If
    Next i
    Application.ScreenUpdating = True
    UpBound = Cells.Rows.Count 'hides all rows after the last used one
    Range(LastRow + 1 & ":" & UpBound).Rows.Hidden = True
 End Sub