Excel VBA - 求和函数

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

Excel VBA - Sum function

vbaexcel-vbaexcel

提问by CustomX

I'm trying to calculate the sum of my columns (column I). From 1 to the last record in I it has. When I record a macro I get this as output, but this is gibberish to me.

我正在尝试计算我的列(第一列)的总和。从 1 到最后一个记录在我它有。当我录制宏时,我将其作为输出,但这对我来说是胡言乱语。

ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-4]C)"

I found another topic and there they said

我找到了另一个话题,他们说

LastRow = .Range("I" & .rows.Count).End(xlUp).row  
Range("I"&LastRow) = "SUM(I1:I...)"

Except in my case, I can't figure how to enter the lastrow of I in it. All help is welcome :)

除了在我的情况下,我无法弄清楚如何在其中输入 I 的最后一行。欢迎所有帮助:)

回答by Widor

There are two ways of referencing a cell - 'R1C1' and 'A1'. The former works like co-ordinates, with a relative number of rows (R) and cells (C).

有两种引用单元格的方法 - 'R1C1' 和 'A1'。前者像坐标一样工作,具有相对数量的行 (R) 和单元格 (C)。

The other reference style refers to the cell name on the sheet - B6, F67 etc.

另一种引用样式是指工作表上的单元格名称 - B6、F67 等。

Let's say you want to put your Sum()in cell B1and LastRowhas a value of 6:

比方说,你想要把你Sum()的细胞B1LastRow具有值6:

ActiveSheet.Range("B1") = "=Sum(I1:I" & LastRow & ")"

Would insert the following function in cell B1:

将在单元格中插入以下函数B1

=SUM(I1:I6)