Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50061

Code takes 10 sec to run in excel 2010 but only 1 sec in older versions, why

$
0
0
When the user deletes an entry in cell B2 etc it takes 10 second to finish filtering (and show all) when running this in Excel 2010 but it is much faster in Excel 2003. What is different? How do I fix it.

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    ' The variable KeyCells contains the cell that will
    ' cause an Advance Filter action when they are changed.
    Set KeyCells = Range("A2:A2")
   
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
          Is Nothing Then

   
      'Automatically cause an Advance Filter action on cell entry
     
' AddressFilter Macro
'
        Range("A3:K4426").AdvancedFilter _
            Action:=xlFilterInPlace, _
            CriteriaRange:=Range("A1:C2"), _
            Unique:=False
        Range("A4").Select
        Range("B2").Select
   
    End If
    Dim KeyCells2 As Range
    Dim rngCell As Range

    ' The variable KeyCells2 contains the cells that will
    ' add a Wilcard to the end of an entry and
    ' cause an Advance Filter action when they are changed.
    Set KeyCells2 = Range("B2:C2")
   
    If Not Intersect(KeyCells2, Range(Target.Address)) Is Nothing Then
   
        'do not trigger event on change
        Application.EnableEvents = False
        For Each rngCell In KeyCells2
            With rngCell
                If Not Right(.Value, 1) = "*" And Len(.Value) > 0 Then
                    .Value = .Value & "*"
                End If
            End With
        Next rngCell
      Application.EnableEvents = True
   
      'Automatically cause an Advance Filter action on cell entry
     
' AddressFilter Macro
'
        Range("A3:K4426").AdvancedFilter _
            Action:=xlFilterInPlace, _
            CriteriaRange:=Range("A1:C2"), _
            Unique:=False
        Range("B2").Select
     
    End If
   
    Set KeyCells = Nothing
    Set KeyCells2 = Nothing

End Sub


Viewing all articles
Browse latest Browse all 50061

Trending Articles