How to reduce runtime for code using ranges and string comparisons

后端 未结 3 2013
礼貌的吻别
礼貌的吻别 2020-12-10 22:45

I have the following code and it does exactly what I need it to do however, the loop takes far too long to run (3 minutes +). I am new to VBA so I am not exactly sure 1) wha

3条回答
  •  天涯浪人
    2020-12-10 23:07

    Suggestion 1

    Replace:

    If Sheets("Sheet1").Range(Cells(i, 3), Cells(i, 3)) = "Police" 
        And Sheets("Sheet1").Range(Cells(i, 14), Cells(i, 14)) = "Bi-wkly Uniform Pay" Then _
      Sheets("Sheet1").Range(Cells(i, 3), Cells(i, 3)) = "Police - Uniform"
    

    by:

    With Sheets("Sheet1")
      If .Range(Cells(i, 3), Cells(i, 3)) = "Police" _
          And .Range(Cells(i, 14), Cells(i, 14)) = "Bi-wkly Uniform Pay" Then _
        .Range(Cells(i, 3), Cells(i, 3)) = "Police - Uniform"
    End With
    

    Suggestion 2

    Replace:

    .Range(Cells(i, 3), Cells(i, 3))
    

    by

    .Cells(i, 3)
    

    Suggestion 3

    Add:

    Application.ScreenUpdating = False
    

    Without this statement, the screen is repainted for every change. That chews up more time than anything else.

提交回复
热议问题