简体   繁体   English

Excel / VBA性能

[英]Excel/VBA performance

I have a certain set of formula in Range("E10001:E20000") which i'd like to copy to Columns("F:CZ") total 1M cells. 我在Range(“ E10001:E20000”)中有一组特定的公式,希望将总共1M单元格复制到Columns(“ F:CZ”)中。 Options: 选项:

  1. Range("E10001:E20000").Copy 范围(“ E10001:E20000”)。复制
    Range("F10001:CZ20000").PasteSpecial xlPasteAll 范围(“ F10001:CZ20000”)。粘贴特殊xlPasteAll
  2. Range("F10001:CZ20000").formula = Range("E10001:E20000").formula Range(“ F10001:CZ20000”)。formula = Range(“ E10001:E20000”)。formula
  3. Range("E10001:CZ20000").fillRight 范围(“ E10001:CZ20000”)。fillRight
  4. Range("E10001:CZ20000").Select 范围(“ E10001:CZ20000”)。选择
    followed by Ctrl+R in Excel 在Excel中按Ctrl + R

At the end, to make the sheet light, i replace formulae with Values, Range("F10001:CZ20000").Value = Range("F10001:CZ20000").Value 最后,为了使表格更轻巧,我将公式替换为Values Range(“ F10001:CZ20000”)。Value = Range(“ F10001:CZ20000”)。Value

What i notice is that Option 4 is way faster than the rest. 我注意到的是,选项4比其他选项快得多。 Can anybody explain the performance penalties in the first 3 options. 谁能解释前三个选项中的性能损失。 Note, i'm unfamiliar with time functions and measured physically using seconds clock. 请注意,我不熟悉时间功能,无法使用秒时钟进行物理测量。

Short answer: Do not use shortcuts with the SendKeys method! 简短答案: 不要将快捷方式与SendKeys方法一起使用!
Longer answer: Shortcuts can lead to unexpected results, in my local settings "^r" would (start to) insert a table. 更长的答案:快捷方式可能会导致意外的结果,在我的本地设置中,“ ^ r”会(开始)插入表格。
Feel free to use a timer with starttime = Now at the beginning of your code and 在代码的开始处随意使用带有starttime = Now的计时器

timetaken = Now - starttime
MsgBox (Hour(timetaken) & ":" & Minute(timetaken) & ":" & Second(timetaken))

to measure the performance though. 衡量性能。
As a rule of thumb, your time is worth way more than system time and corrupting your data by accidentally sending the wrong set of instructions is never worth the risk. 根据经验,与系统时间相比,您所花费的时间值得多得多,并且由于意外发送错误的指令集而破坏数据的风险绝对不值得。
Your operation with 2 × 10,000 cells is not worth optimising. 您对2×10,000单元的操作不值得优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM