隐藏没有VBA值的行

隐藏没有VBA值的行,vba,Vba,我试图编写一个宏,它遍历一个列表,如果该行的任何列中都没有值,则隐藏该行。但是,数据已经具有来自另一个宏的隐藏列。下面是我的尝试,到目前为止似乎没有任何效果。任何帮助都将不胜感激 Sub HideRowsMenu() BeginRow = 5 'Start after Master Menu item EndRow = 731 'Filter all rows in sheet (about 730) ColumnsWithValues = 0 'Counter for

我试图编写一个宏,它遍历一个列表,如果该行的任何列中都没有值,则隐藏该行。但是,数据已经具有来自另一个宏的隐藏列。下面是我的尝试,到目前为止似乎没有任何效果。任何帮助都将不胜感激

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

你不喜欢这种工作吗?(未经测试)


我试图编辑代码,但它说更改一个部分是不够的lol。您的代码可以工作我测试了它,但您需要更新此设置您的设置rowrange以设置rowrange=Range(单元格(RowNumber,ColumnStart),单元格(RowNumber,ColumnEnd))。SpecialCells(xlCellTypeVisible)这似乎工作得很好。谢谢你的帮助。我想根据选择的范围来做,但不确定如何区分可见/不可见。不客气!如果问题解决了,请将答案标记为“已接受”。干杯!对上面的评论也是肯定的,有一段时间应该有一个逗号。是的,我注意到了这一点,现在修复了:)
'## 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