简体   繁体   中英

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"

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.

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. use .Address property to get the address you need in a formula. for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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