简体   繁体   中英

Show row based on cell value - Excel VBA

I'm using the following code to hide rows based on cell value:

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

What do I have to modify if I also want to unhide the rows where the cell value is 1?

Thanks in advance!

MD

This will hide all that are 0 and unhide all others.

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

Of course you can do the same with a filter.

You could just add an additional If statement, no?:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM