我隐藏了列8中值为"Kitchen“的所有行。
如果第12列包含"No“,我还需要隐藏行。这是一个"Or“语句,不是一个"And”语句。
我已经研究了这个网站,但还没有找到答案。我也想加快这一过程。
Sub FOHc()
BeginRow = 6
EndRow = 400
ChkCol = 8
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End Sub
发布于 2016-01-29 02:45:13
您只需再添加一个Or
条件
If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Or Cells(RowCnt, ChkCol2).Value = "No" Then
为了提高宏的速度,可以在开头添加Application.ScreenUpdating = False
,在末尾添加Application.ScreenUpdating = True
。这将在继续执行宏时停止更新屏幕。
发布于 2016-01-29 02:48:51
要添加另一个逻辑测试,只需添加另一个Or
语句。
为了加快收集所有行以隐藏在一个范围内,并一次性隐藏它们。
一般来说,使用适当的限定符也是一个好主意,语句Cells
指的是Excel认为是活动工作表的内容。而ThisWorkbook.Worksheets("Sheet1").Cells
总是引用名为"Sheet1“的工作表。
声明所有变量可以防止不必要的行为和错误,通常是一种好的做法。
以下是重构后的代码。
Sub FOHc()
Dim beginRow As Long
Dim endRow As Long
Dim chkCol As Long
Dim rowCnt As Long
Dim rngResult As Range
Dim ws As Worksheet
beginRow = 6
endRow = 400
chkCol = 8
Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change this to the actual name of your sheet.
With ws
.Cells.EntireRow.Hidden = False 'Unhides all rows, remove line if that's not desired
For rowCnt = beginRow To endRow
If .Cells(rowCnt, chkCol) = "Kitchen" Or .Cells(rowCnt, chkCol) = Empty Or .Cells(rowCnt, 12) = "No" Then
If rngResult Is Nothing Then
Set rngResult = .Cells(rowCnt, 1)
Else
Set rngResult = Union(rngResult, .Cells(rowCnt, 1))
End If
End If
Next rowCnt
End With
If Not rngResult Is Nothing Then rngResult.EntireRow.Hidden = True
End Sub
https://stackoverflow.com/questions/35069225
复制相似问题