隐藏没有VBA值的行

时间:2014-06-02 14:32:36

标签: vba

我正在尝试编写一个遍历列表的宏,如果该行的任何列中没有值,则隐藏一行。但是,数据已经隐藏了另一个宏的列。这是我在下面的尝试,它似乎没有做任何事情。任何帮助将不胜感激。

Sub HideRowsMenu()
    BeginRow = 5 'Start after Master Menu item
    EndRow = 731 'Filter all rows in sheet (about 730)
    ColumnsWithValues = 0 'Counter for number of columns in a row that have a value. If 0, hide the row.
    ColumnStart = 2 'Start where you  have group values
    ColumnEnd = 50 'Maximum number of groups

    RowNumber = 0
    ColumnNumber = 0

    'Outer loop cycles through all the rows of range, inner cycles through the columns to check values
    For RowNumber = BeginRow To EndRow
        ColumnsWithValues = 0 'Reset counter to 0 to avoid counting last row's values

        For ColumnNumber = ColumnStart To ColumnEnd
            'If given cell index is empty (0) and the cell is not previously hidden, add 1 to the counter
            If Cells(RowNumber, ColumnNumber).Value = 0 And Cells(RowNumber, ColumnNumber).Columns.Hidden = False Then
                ColumnsWithValues = ColumnsWithValues + 1
            End If
        Next ColumnNumber

        'After going through all the columns of a row, check if there were any column with values. If not, then hide the row
        If ColumnsWithValues = 0 Then
            Cells(RowNumber, ColumnNumber).EntireRow.Hidden = True
        End If
    'Repeat for all rows
    Next RowNumber
End Sub

1 个答案:

答案 0 :(得分:0)

不会有这样的工作吗? (另)

'## Declare a range variable to represent the current row'
Dim rowRange as Range
For RowNumber = BeginRow To EndRow
    '# Set the range variable, only the VISIBLE cells'
    Set rowRange = Range(Cells(RowNumber, ColumnStart), Cells(RowNumber, ColumnEnd)).SpecialCells(xlCellTypeVisible)
    '# use magic to determine if there are any values in this row
    If Application.WorksheetFunction.CountA(rowRange) = 0 Then
        rowRange.EntireRow.Hidden = True
    End If
Next RowNumber