2016-03-02 104 views
1

我以前錄製宏函數來創建此,但它運行很慢,我想看看是否有人對如何清理任何想法。看起來好像我在這裏做兩件事情做同樣的事情?提前致謝。清理代碼宏

Sub Activations() 
' 
' Master_Button2_2_Click Macro 
' 

' 
Application.ScreenUpdating = False 
Sheets("Index").Select 
Columns("A:C").Select 
ActiveWorkbook.Worksheets("Index").Sort.SortFields.Clear 
ActiveWorkbook.Worksheets("Index").Sort.SortFields.Add Key:=Range("B2:B12000" _ 
    ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 
With ActiveWorkbook.Worksheets("Index").Sort 
    .SetRange Range("A1:C12000") 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
End With 
Sheets("Duplicates").Select 
ActiveSheet.Range("$L$4:$N$3476").AutoFilter Field:=1, Criteria1:= _ 
    "Activate" 
Sheets("Master").Select 
ActiveSheet.Range("$A$2:$BU$11965").AutoFilter Field:=73, Criteria1:= _ 
    "A" 
Application.ScreenUpdating = True 
End Sub 

回答

0

你的代碼是相當簡單的,儘管它不完全清楚它試圖完成什麼。這是一個快速重寫,把幾個With ... End With statement用來縮小受影響的工作區域。

Sub Activations() 
    ' Master_Button2_2_Click Macro 
    Dim lr As Long 

    appTGGL bTGGL:=False 

    With Worksheets("Index") 
     lr = .Cells.SpecialCells(xlCellTypeLastCell).Row 
     With .Range("A1:C" & lr) 
     .Cells.Sort Key1:=.Columns(2), Order1:=xlAscending, _ 
        Orientation:=xlTopToBottom, Header:=xlYes 
     End With 
    End With 

    With Worksheets("Duplicates") 
     If .AutoFilterMode Then .AutoFilterMode = False 
     lr = .Cells.SpecialCells(xlCellTypeLastCell).Row 
     With .Range("L4:N" & lr) 
      .AutoFilter Field:=1, Criteria1:="activate" 
     End With 
    End With 

    With Worksheets("Master") 
     If .AutoFilterMode Then .AutoFilterMode = False 
     lr = .Cells.SpecialCells(xlCellTypeLastCell).Row 
     With .Range("A2:BU" & lr) 
      .AutoFilter Field:=73, Criteria1:="A" 
     End With 
     .Select 
    End With 

    appTGGL 
End Sub 

Sub appTGGL(Optional bTGGL As Boolean = True) 
    With Application 
     .EnableEvents = bTGGL 
     .ScreenUpdating = bTGGL 
     .DisplayAlerts = bTGGL 
     .Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual) 
    End With 
End Sub 

樣本數據和說明的簡要敘述會幫助這個問題。我們並不都講英文,但我們都會講代碼和數據。