I have a worksheet with many formulas that look one cell above, or to the right. If I use ctrl-D to populate rows, the formulas are updated correctly (so =B1 + A2 in B2 becomes =B2+A3 in B3). But when I insert in between rows, things get messed (so inserting a row above B3, moving it to B4 makes its formula become =B2+A4, which is not what I want)
So I thought to create a CellAbove and CellRight formulas (so I can write =CellAbove() + CellRight()). How can I do that?
Function CellAbove() CellAbove = [Address(Row() - 1, Column())].Value End Function
The function below works, but cells that have it are not updated:
Function CellAbove() CellAbove = Range([Address(Row() - 1, Column())]) End Function
So if A1 has 2 and A2 has =CellAbove() then A2 will show 2. But if now I change A1 to 3, then A2 will still show 2
Since your function worked, and it seemed all you needed was it to recalculate when you insert rows.
“but cells that have it are not updated”
User Defined Functions are by default NOT volatile so they won’t automatically recalculate.
You could change your existing function to this.
Function CellAbove() Application.Volatile (True) CellAbove = Range([Address(Row() - 1, Column())]) End Function
This can impact performance, but seems to be what you are asking for.
=INDIRECT(“R”&ROW()-1&”C”&COLUMN(),FALSE) would work as a function.
Suppose you’re on B2. Then the bit inside the brackets would evaluate to R1C2. The INIDRECT function would then calculate what is at row 1 column 2 (you need the FALSE to ensure you’re using the R1C1 style of cell address).
I’m sure you could do something simpler using:
but I can’t see how you can refer to the current cell.
Followup on my comment. We don’t need vba for this 🙂
After you have inserted the blank row
Drag the formula down from
B3 or simply select
B3 and press
B3. Do an Autofill by double clicking on the bottom right corner of the cell.
You could do this using built-in Excel functions like INDIRECT, ADDRESS, ROW, and COLUMN, as in…
…for the cell above, or…
…for the cell to the right.
Another approach, again using built-in capability, would be to use the OFFSET function. For example, in cell B2, you could enter the formula…
…for the cell above, or…
…for the cell to the right. It looks like a circular reference, but it’s not, since you are not referencing the value of the same cell, only its address.
Either way, you more or less lose the ability to use the “Trace Dependents” and “Trace Precedents” features, since each cell essentially references only itself.
Functions from yours
Function CellAbove() CellAbove = Range([Address(Row() - 1, Column())]) End Function Function CellRight() CellRight = Range([Address(Row(), Column() + 1)]) End Function
May be called as
Function test000000() ActiveCell = CellAbove + CellRight End Sub