[英]Show row based on cell value - Excel VBA
我正在使用以下代码根据单元格值隐藏行:
Sub HideN()
Dim RowCnt As Long, uRng As Range
BeginRow = 8
EndRow = 232
ChkCol = 6
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 0 Then
If uRng Is Nothing Then
Set uRng = Cells(RowCnt, ChkCol)
Else
Set uRng = Union(uRng, Cells(RowCnt, ChkCol))
End If
End If
Next RowCnt
If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True
End Sub
如果我也想取消隐藏单元格值为1的行,该怎么办?
提前致谢!
MD
这将隐藏所有0,并取消隐藏所有其他。
Sub HideN()
Dim RowCnt As Long
Dim BeginRow&, EndRow&, ChkCol&
BeginRow = 8
EndRow = 232
ChkCol = 6
For RowCnt = BeginRow To EndRow
Rows(RowCnt).Hidden = Cells(RowCnt, ChkCol).Value = 0
Next RowCnt
End Sub
当然,您可以使用过滤器执行相同的操作。
您可以只添加一个附加的If
语句,否?:
Sub HideN()
Dim RowCnt As Long, uRng As Range
BeginRow = 8
EndRow = 232
chkcol = 6
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, chkcol).Value = 0 Then
If uRng Is Nothing Then
Set uRng = Cells(RowCnt, chkcol)
Else
Set uRng = Union(uRng, Cells(RowCnt, chkcol))
End If
End If
If Cells(RowCnt, chkcol).Value = 1 Then ' This is the new line to add
Rows(RowCnt).EntireRow.Hidden = False
End If
Next RowCnt
If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.