How to improve the speed of VBA macro code?

前端 未结 7 1542
孤城傲影
孤城傲影 2020-12-06 07:11

I do not have much experience with writing macros, and therefore need the help of this community for the following issue encountered:

My macro copies a range of valu

7条回答
  •  臣服心动
    2020-12-06 07:25

    As suggested by a few others in the comments, you should definitely change Application.Calculation to xlCalculationManual and rememeber to set it back to xlcalculationAutomatic at the end. Also try setting Application.Screenupdating = False (and turning that back on again too). Also, bear in mind that .Copy is a very inefficient way to copy cell values - if you really just want the values, loop through the range setting .Value to the .Values in the old range. If you need all the formatting, you're probably stuck with .Copy.

    When you turn off the calc/screen refresh flags, please remember to turn them back on in all circumstances (even when your program exits at a different point, or causes a runtime error). Otherwise all sorts of bad things will happen. :)

提交回复
热议问题