显示基于单元格值的行 – 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

你可以添加一个额外的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 

这将隐藏所有的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 

当然你也可以用一个filter来做同样的事情。