+ Reply to Thread
Results 1 to 3 of 3

Auto Filter is slowing the macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Auto Filter is slowing the macro

    I have a macro that works fine and but the only problem is that the autofilter is slowing the down the macro. Is there another way of doing the autofilter to make the marco faster? The macro is attched.
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Auto Filter is slowing the macro

    I think the problem is the "activating", since most of your commands were addressing sheets directly, I corrected the few that weren't and removed all the selecting, this should run noticably faster.
    Sub SortTwoWayNeigbors()
    
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim ws4 As Worksheet, ws5 As Worksheet, ws6 As Worksheet
    Dim i As Long, j As Long, LR1 As Long, LR2 As Long
    Dim A As String
    Application.ScreenUpdating = False
    
    Set ws1 = Sheets("Tow_Way_Neighbors")
    'Set ws2 = Sheets("Process_Data")
    Set ws3 = Sheets("Intra_Relations")
    Set ws4 = Sheets("UtranRelation")
    Set ws5 = Sheets("Run_Tool")
    ws4.Cells(1, 1) = "UtranCell"
    On Error Resume Next
    
    j = ws3.Cells(ws3.Rows.Count, "A").End(xlUp).Row
    
    For s = j To 2 Step -1
        i = ws3.Cells(s, ws3.Columns.Count).End(xlToLeft).Column
    
        For m = 3 To i Step 1
            UtranCellUMTS = ws3.Cells(s, m).Text
            LR2 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
            With ws4    'ws4.Activate
                .AutoFilterMode = False
                .Range("A:A").AutoFilter
                LR3 = .Cells(.Rows.Count, "A").End(xlUp).Row
                With .Range(.Cells(2, 1), .Cells(LR3, 1))
                    Set c = .Find(UtranCellUMTS, LookIn:=xlValues)
                    If Not c Is Nothing Then
                        .Range("A:A").AutoFilter Field:=1, Criteria1:="=" & UtranCellUMTS & "*"
                        .Range("A2:A" & LR3).EntireRow.Copy
                        With ws1.Cells((LR2 + 1), 1)
                            .PasteSpecial xlPasteValues
                            .PasteSpecial xlPasteFormats
                        End With
                    Else
                        ws3.Cells(s, m).Interior.ColorIndex = 3
                        GoTo line1
                    End If
                End With
            End With
    
    '''''''''''''''''''''''''''Finding the Two Way Neighbors'''''''''''''''''''''
    
            On Error Resume Next
            varLU = ws3.Cells(s, 2)
            With ws1
                l = .Cells(.Rows.Count, "A").End(xlUp).Row
                With .Range(.Cells(2, 1), .Cells(l, 2))
                    On Error Resume Next
                    Set c = .Find(varLU, LookIn:=xlValues)
                    If Not c Is Nothing Then
                        VarLU2 = c
                        If varLU = VarLU2 Then
                            RowNo = c.Row
                            ws3.Cells(s, m).Interior.ColorIndex = 6
                        End If
                    Else
                        ws3.Cells(s, m).Interior.ColorIndex = 3
                    End If
                End With
            
                .Cells.Clear
            End With
    line1:
        Next m
    Next s
    
    ws4.AutoFilterMode = False
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Re: Auto Filter is slowing the macro

    Thanks for your help it worked perfectly. I really appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1