[英]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.