1

I'm handling Change event in Excel using intersect like this:

Sub Worksheet_Change(ByVal Target As Range)
    Dim WatchRange As Range
    Dim IntersectRange As Range
    Set WatchRange = Range("B2:B65536")
    Set IntersectRange = Intersect(Target, WatchRange)

    If IntersectRange Is Nothing Then
        'Do Nothing
    Else
        For Each b In Target.Rows
            Range("A" & b.Row).Value = "*"
        Next
    End If
End Sub

When I change something in column B, it writes * to column A. And it works great.

Problem happens when I set the filter and copy some value to filtered rows using CTRL+D. This is my test table:

enter image description here

Filter only text in column C:

enter image description here

And copy number 100 using CTRL+D to all visible rows (column B):

enter image description here

When I cancel the filter, all the rows are marked with *:

enter image description here

It wouldn't be a problem in small table, but it takes about 10sec in table with about 1000 rows.

Is it possible to skip hiden files somehow?

gaffcz
  • 3,469
  • 14
  • 68
  • 108

1 Answers1

1

Use .EntireRow.Hidden to figure out if particular cell is filtered or not. Your code would look like:

Sub Worksheet_Change(ByVal Target As Range)
    Dim WatchRange As Range
    Dim IntersectRange As Range
    Set WatchRange = Range("B2:B65536")
    Set IntersectRange = Intersect(Target, WatchRange)

    If IntersectRange Is Nothing Then
        'Do Nothing
    Else
        For Each b In Target
            If b.EntireRow.Hidden = False Then
                Range("A" & b.Row).Value = "*"
            End If
        Next
    End If
End Sub
Limak
  • 1,511
  • 3
  • 12
  • 22