简体   繁体   English

VBA插入公式与动态单元格引用

[英]VBA insert formula with dynamic cell reference

I am creating subtotals and would like to have in the empty cells (no sum) of the subtotal line reference the cell above. 我正在创建小计,并希望小计行的空单元格(无总和)中引用上面的单元格。 The formula would reference the cell above. 该公式将引用上面的单元格。 For example: if row 4 is the subtotal line and cell A4 is an empty cell than I was the formula in A4 to read "=A3" 例如:如果第4行是小计行,并且单元格A4是一个空单元格,则比我在A4中的公式要读取“ = A3”

ActiveCell.Formula = "=" & Cells(-1, "A").Address(False, False)

I have the above code written but I get Run-time error 1004 Application -defined or object defined error. 我已经编写了上面的代码,但遇到运行时错误1004应用程序定义的错误或对象定义的错误。

I have also tried the code below but error out as well. 我也尝试了下面的代码,但也出错了。

ActiveCell.Formula = "=" & Cells(-1, A).Address(False, False)

Use the offset to reference a cell relative to another. 使用偏移量来引用相对于另一个单元格。

ActiveCell.Offset(-1, 0)

if the activecell is A4 then the offset refers to A3. 如果活动单元为A4,则偏移量为A3。 use .Address property to get the address you need in a formula. 使用.Address属性获取公式中所需的地址。 for example: 例如:

ActiveCell.Formula = "=" & ActiveCell.Offset(-1, 0).Address

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

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