简体   繁体   中英

Excel VBA - Look down column A for specific value and hide all rows below activecell row?

I am using the following vba script to hide all rows with the matching cell value in column A of the Active Cell row:

If Not Intersect(ActiveCell, Range("I:I")) Is Nothing And ActiveCell.Value = "-" Then

 BeginRow = 1
    EndRow = 50000
    ChkCol = 1

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = ActiveCell.Offset(1, -8) Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt

End If

So for instance if i have 5 rows all with the number 35 in column A like so:

A            I
35           Click to hide   <----- First Row
35           
35           
35           
35           

Then all these rows will be hidden. However, i don't want the row of the first instance to be hidden.

Add two variables (a variant to hold the value to be compared and a boolean to validate the first value found)

Dim vCllValue As Variant
Dim bl1stVal As Boolean

This is the revised code:

If Not Intersect(ActiveCell, Range("I:I")) Is Nothing And ActiveCell.Value = "-" Then
    BeginRow = 1
    EndRow = 50000
    ChkCol = 1

    bl1stVal = False
    vCllValue = ActiveCell.Offset(1, -8).Value
    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = vCllValue Then
            If Not (bl1stVal) Then
                bl1stVal = True
            Else
                Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If: End If: Next: End If

Noticed that the code compares the value in the row below the ActiveCell I'm assuming that part is correct, otherwise let me know and will do the necessary adjustments.

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