VBA,如果每个行中没有数据,我如何隐藏整行和整列?

时间:2017-07-28 09:37:13

标签: vba

我对VBA很新。行和列都有标题,如果它们分别没有任何数据,我需要隐藏它们。

1 个答案:

答案 0 :(得分:0)

应该做的伎俩

Sub main()

    Sheets("Sheet1").Activate

    Dim i As Integer
    Dim firstColumn, lastColumn, firstRow, lastRow As Integer

    firstColumn = 1
    lastColumn = 10

    firstRow = 1
    lastRow = 10

    ' Check if column has any data beside its header
    For i = firstColumn To lastColumn
        Columns(i).Hidden = (WorksheetFunction.CountA(Columns(i)) < 2)
    Next i

    ' Check if row has any data beside its header
    For i = firstRow To lastRow
        Rows(i).Hidden = (WorksheetFunction.CountA(Rows(i)) < 2)
    Next i

End Sub