A Custom Function For Relative Sheet ReferencesYou may have discovered that Excel's support for "3D workbooks" is limited. For example, if you need to refer to a different worksheet in a workbook, you must include the worksheet's name in your formula. This is not a big problem -- until you attempt to copy the formula across other worksheets. The copied formulas continue to refer to the original worksheet name. This tip contains a VBA function (named SHEETOFFSET) that lets you address worksheets in a relative manner. For example, you can refer to cell A1 on the previous worksheet using this formula: =SHEETOFFSET(-1,A1) Then, you can copy this formula to other sheets and the relative referencing will be in effect in all of the copied formulas. The SHEETOFFSET FunctionThe VBA code for the SHEETOFFSET function is listed below. Function SHEETOFFSET(offset, Ref)
' Returns cell contents at Ref, in sheet offset
Application.Volatile
With Application.Caller.Parent
SHEETOFFSET = .Parent.Sheets(.Index + offset) _
.Range(Ref.Address).Value
End With
End Function
Using the SHEETOFFSET functionTo use this function in a worksheet, just copy the code and paste it to a VBA module. Then, you can use formulas such as: =SHEETOFFSET(2,C1)
Thanks to Bill Manville for suggesting a correction to the original function posted here.
|