0
votes

I have a worksheet that returns a value from a different sheet in the sameworkbook (=data!C68) which returns a value.

Ina another cell I want to reference the reference in the original cell but increment the row by 1 - (=data!C69). The cells are not next to each other.

Is this possible either with a function or a fromula?

Thanks

Jonathan

2

2 Answers

2
votes

There are many ways to achieve this. Here is one way. I have extended the function by incorporating the rows and columns that you want to Offset. Paste this code in a module

Syntax

=GetOffset(Range,Row,Col)

Remarks

Range (Required) is the cell which has the formula

Row (Optional) Row(s) you want to offset

Col (Optional) Column(s) you want to offset

Sample Usage

=GetOffset(A1) : This will pickup the value from the same cell which A1 is referring to. If A1 is referring to =data!C69 then GetOffset will get value from data!C69

=GetOffset(A1,1) : This will pickup the value from the next cell (1 row down) which A1 is referring to. If A1 is referring to =data!C69 then GetOffset will get value from data!C70

=GetOffset(A1,1,1) : This will pickup the value from the next cell (1 row down across 1 Col) which A1 is referring to. If A1 is referring to =data!C69 then GetOffset will get value from data!D70

=GetOffset(A1,0,1) : This will pickup the value from the next cell (Same row across 1 Col) which A1 is referring to. If A1 is referring to =data!C69 then GetOffset will get value from data!D69

CODE

Public Function GetOffset(Rng As Range, Rw As Long, Col As Long) As Variant
    Dim MyArray() As String, strTemp As String
    Dim TempRow As Long, TempCol As Long

    On Error GoTo Whoa

    Application.Volatile

    MyArray = Split(Rng.Formula, "!")

    TempRow = Range(MyArray(UBound(MyArray))).Row
    TempCol = Range(MyArray(UBound(MyArray))).Column

    TempRow = TempRow + Rw
    TempCol = TempCol + Col

    strTemp = MyArray(0) & "!" & Cells(TempRow, TempCol).Address

    GetOffset = Application.Evaluate(Trim(strTemp))
    Exit Function
Whoa:
End Function
2
votes
Option Explicit
Function GetCellReferenceUsedInFormula(ByVal src As Range) As Range
If src.HasFormula Then
    Set GetCellReferenceUsedInFormula = Evaluate(Mid(src.Formula, 2))
End If
End Function

And use the above function in formula with OFFSET

=OFFSET(GetCellReferenceUsedInFormula(A1),1,0)

This is assuming that cell A1 contains the formula =data!C68.
Please note that the VBA code assumes that the formula is simple (i.e. =somecellreference).