使宏运行得更快

时间:2014-03-12 18:46:02

标签: performance vba excel-vba excel

我有这个宏,我在Excel中运行,但运行速度非常慢,任何消化都会让它运行得更快?

它运行了将近30分钟,我知道我的计算机不是最新的,但它有相当多的时间。

Sub Macro1first_part()
    Sheets("IEX Adherence").Select    
    'Deletes all previous data
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
    ActiveSheet.paste
    'Start of Macro sets the Search value of  "Signon" to be the number of times the macro loops
    Dim Search As String    
    'Insert Column
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
    'Paste formula to look for Agnet Id        
    ActiveCell.FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[1],'Agent DataBase'!C6,1,0),"" "")"
    Range("A1").Select
    Selection.Copy
    Columns("A:A").Select
    ActiveSheet.paste
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

   'Format Column to be able to work with Ids

    Rows("1:1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A:$AB").AutoFilter Field:=1, Criteria1:="="
    Columns("A:A").Select
    Selection.ClearContents
     ActiveSheet.Range("$A:$AB").AutoFilter Field:=1
    ActiveWindow.SmallScroll Down:=-21
    ActiveSheet.Range("$A:$AB").AutoFilter Field:=2, Criteria1:="Signon"
    Columns("B:B").Select
    Selection.ClearContents
    ActiveSheet.Range("$A:$AB").AutoFilter Field:=2
    ActiveSheet.Range("$A:$AB").AutoFilter Field:=10, Criteria1:="Signon"
    Columns("J:J").Select
    Selection.ClearContents

    ActiveWindow.SmallScroll Down:=-15

    ActiveSheet.Range("$A:$AB").AutoFilter Field:=10

    Range("A1").Select
    'The actual search for the word "Signon"
     Dim wordCount As Long

    wordCount = Application.WorksheetFunction.CountIf(ActiveSheet.Cells, "Signon")

    For wordCount = 1 To 230
    Cells.Find(What:="Signon", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
        ActiveCell.Offset(0, -3).Select
        Selection.Copy
        Selection.End(xlToLeft).Select
        Selection.End(xlToLeft).Select
        Selection.End(xlUp).Select  
        ActiveCell.Offset(0, 6).Select            
        ActiveSheet.paste
        ActiveCell.Offset(15, 1).Select                             Cells.FindNext(After:=ActiveCell).Activate
          Debug.Print wordCount
Next wordCount    
'macro finishes loop activitie
 Sheets("Agent DataBase").Select
 Range("A1").Select

MsgBox "Adherence Update Completed!"

End Sub

1 个答案:

答案 0 :(得分:1)

开始摆脱所有.SELECTS!

例如..

 Rows("1:1").Select
    Selection.AutoFilter

可以

Rows("1:1").AutoFilter

还会在宏的开头关闭屏幕更新并在结束时重新开启

Application.ScreenUpdating = False

Application.ScreenUpdating = True

这将是一个良好的开端!