Home » excel » excel – Easily reference cells in offset

excel – Easily reference cells in offset

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

My attempt:

Function CellAbove()
   CellAbove = [Address(Row() - 1, Column())].Value
End Function

doesn’t work.

UPDATE:
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

How to&Answers:

Since your function worked, and it seemed all you needed was it to recalculate when you insert rows.
you said:

“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.

Answer:

=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:

OFFSET(CurrentCell,-1,0)

but I can’t see how you can refer to the current cell.

Answer:

Followup on my comment. We don’t need vba for this 🙂

After you have inserted the blank row

Step 1

Drag the formula down from B2 to B3 or simply select B3 and press CTL D

Step 2

Select Cell B3. Do an Autofill by double clicking on the bottom right corner of the cell.

enter image description here

Answer:

You could do this using built-in Excel functions like INDIRECT, ADDRESS, ROW, and COLUMN, as in…

=INDIRECT(ADDRESS(ROW()-1,COLUMN()))

…for the cell above, or…

=INDIRECT(ADDRESS(ROW(),COLUMN()+1))

…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…

=OFFSET(B2,-1,0)

…for the cell above, or…

=OFFSET(B2,0,1)

…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.

Answer:

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