How to reduce runtime for code using ranges and string comparisons

后端 未结 3 2012
礼貌的吻别
礼貌的吻别 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:16

    This may not be the best answer, but try setting local variable

    Var sht1 = Sheets("Sheet1") might reduce ever so slightly the selecting of the sheet object. Also, there's no need to select the range and the cells using (i, 3) since it's a range of a single cell, so combined you'd have something like

     If sht1.Range.Cells(i,3) = "Police" And sht1.Range.Cells(i,14) = "Bi-wkly Uniform Pay" Then sh1.Range.Cells(i,3) = "Police Uniform" 
    Next i
    

    If that doesn't work and you can put it in a different column (say column O or 15), then you simply use a formula and then drag/double-click, or you could use an array formula over the entire column similarly and then press ctrl + shift + enter to make it compute as an array formula.

    Hope this helps even a bit.

提交回复
热议问题