VBA 将公式应用于大范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19067083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:33:02  来源:igfitidea点击:

VBA apply formula to large range

excelvbaexcel-vba

提问by Louisa Thompson

I have a large range (upto 60k lines) which I would like to apply a formula too.

我有一个很大的范围(最多 60k 行),我也想应用一个公式。

A loop would take too long. The formula would apply to info on the row so wouldn't be the same exactly but the row would be the only variable.

一个循环会花费太长时间。该公式将应用于行上的信息,因此不会完全相同,但行将是唯一的变量。

What's the best way to do this? I thought I would use VBA rather than Excel to make sure the spreadsheet didn't get too cumbersome.

做到这一点的最佳方法是什么?我想我会使用 VBA 而不是 Excel 来确保电子表格不会变得太麻烦。

回答by user3357963

Below is a simple way to do what you want. If the formula takes too long to update it may be better to calc the result using VBA and output the result to the range.

下面是一个简单的方法来做你想做的事。如果公式更新时间过长,最好使用 VBA 计算结果并将结果输出到范围内。

Assume you want the formula =INDEX($M:$M,MATCH(A2,$N:$N,0),0)in rows D2:D60000

假设您想要=INDEX($M:$M,MATCH(A2,$N:$N,0),0)D2:D60000 行中的公式

i. Turn on the macro recorder

一世。打开宏记录器

ii. Enable Use Relative References

ii. 启用使用相对引用

enter image description here

在此处输入图片说明

iii. Enter the formula required in a single cell (ensure if dragged down it will be correct for all rows)

三、在单个单元格中输入所需的公式(确保向下拖动它对所有行都是正确的)

iv. Turn off the macro recorder

四、关闭宏记录器

v. In the VB Editor find the formula recorded - eg

v. 在 VB 编辑器中找到记录的公式 - 例如

ActiveCell.FormulaR1C1 = "=INDEX(C13,MATCH(RC[-5],C14,0),0)"

vi. Apply the formula to the required range using

六. 使用公式将公式应用于所需的范围

Sheet1.range("D2:D60000").FormulaR1C1 = "=INDEX(C13,MATCH(RC[-5],C14,0),0)"