如何根据工作表最后一列中的单元格值隐藏行?

时间:2019-02-11 13:28:49

标签: excel vba

我正在尝试创建一个宏,在该宏中搜索工作表的最后一列以寻找一个值,如果在某个单元格中找到该值,则该单元格的整个行都将被隐藏。我还尝试为此使用动态范围,因为最后一列会更改。

With ws1

LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, LastColumn).End(xlUp).Row

Set rDataRange = .Range(.Cells(2, LastColumn), .Cells(LastRow, LastColumn))

For Each rCell in RDataRange 
    If rCell.Value = "Yes" Then
        rCell.EntireRow.Hidden = True
    End If
Next rCell
End With

任何人都知道为什么这可能不起作用?

1 个答案:

答案 0 :(得分:1)

尝试:

Option Explicit

Sub test()

    Dim LastColumn As Long, Lastrow As Long, i As Long
    Dim str As String

    With ThisWorkbook.Worksheets("Sheet1")

        str = "Test"
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Lastrow = .Cells(.Rows.Count, LastColumn).End(xlUp).Row

        For i = 1 To Lastrow

            If .Cells(i, LastColumn).Value = str Then
                .Rows(i).EntireRow.Hidden = True
            End If

        Next i

    End With

End Sub