简体   繁体   English

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

[英]How to hide row based on cell value in last column of worksheet?

I'm trying to create a macro where the last column of a worksheet is searched for a value, and if that value is found in a cell that cell's entire row is hidden. 我正在尝试创建一个宏,在该宏中搜索工作表的最后一列以寻找一个值,如果在单元格中找到该值,则该单元格的整个行都将被隐藏。 I'm also trying to use a dynamic range for this as the last column will change. 我还尝试为此使用动态范围,因为最后一列会更改。

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

Anyone have an idea as to why this might not be working? 任何人都有一个想法,为什么这可能不起作用?

Try: 尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于行值自动隐藏列,行值基于相对工作表中的if函数? - How to automatically hide column based on row value, which is based on an if function from an opposing worksheet? 如何基于特定列而不是可变行中单元格的值命名工作表,同时创建/复制工作表? - How can I create/copy a worksheet while naming it based on the value of a cell in a specific column but variable row? 使用VBA根据不同工作表中的单元格值隐藏工作表中的行 - Hide rows in a worksheet based on a cell value in different worksheet using VBA 根据另一个工作表中的单元格值隐藏一个工作表中的行 - Hide rows in one worksheet based on a cell value in another worksheet 根据行中的值自动隐藏/取消隐藏列,该值是从另一个工作表转换而来的公式 - Automatically Hide/Unhide Column's based on value in row that is formula transposed from another worksheet 如何根据另一个工作表上的单元格值在多个工作表中隐藏/显示行 - How to hide / show rows across multiple worksheets based on a cell value on another worksheet 如何在另一个工作表上查找单元格值,并将值放在找到第一个值的行的另一列上 - How to find a cell value on another worksheet and place a value on another column for the row where the first value was found 如何根据单元格值重命名工作表 - How to rename a worksheet based on a cell value 根据两个值更新单元格以查找行和列以及相应的工作表 - Update cell based on two values for finding the row and column and corresponding worksheet 根据不同工作表中的单元格值在一个工作表上删除一行 - Deleting a row on one worksheet based on a cell value in a different sheet
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM