Excel VBA:如何将公式插入单元格区域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7168017/
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-08 11:48:34  来源:igfitidea点击:

Excel VBA: How to Insert formula into an area of cells?

excelvba

提问by EKet

Problem

问题

I need to insert some formulas into a 2 dimensional area of cells with VBA. For example below

我需要使用 VBA 将一些公式插入到二维单元格区域中。例如下面

Excel Range sample

Excel 范围示例

Of course I can do this for 1 row with VBA using the .Formulas = "=Sum(....)"

当然,我可以使用 .Formulas = "=Sum(....)" 使用 VBA 对 1 行执行此操作

but how can I include an area instead of just a range? This seems so trivial but I can't seem to figure it out. I'm trying to avoid using a loop to fill out every row.

但是我怎样才能包括一个区域而不仅仅是一个范围呢?这似乎微不足道,但我似乎无法弄清楚。我试图避免使用循环来填写每一行。

回答by Lance Roberts

Range("yourrange").Formula = "=Sum(whatever)"

where yourrangeis a Named Range, or for an actual reference

其中yourrange是命名范围,或用于实际参考

Range("A1:F8").Formula = "=Sum(whatever)"

回答by Banjoe

range("A1:G10").formula = "=Sum(H1:H10)"

range("A1:G10").formula = "=Sum(H1:H10)"

Fill in your own range/formula

填写您自己的范围/公式