Formula that adjusts then evaluates a formula

scifibum

Well-known Member
Joined
Jul 22, 2004
Messages
503
I'm thinking there should be a worksheet function (or UDF) that will essentially link to another formula, but evaluate it with adjusted ranges.

For example, let's say I build a formula in Sheet1, cell A1 which I will then want to duplicate in several places. For argument's sake it's:

=SUM(Sheet1!B1:C1)

(In reality it would be something more complicated.)

In other locations in the workbook I'd like to be able to do this:

=formula(Sheet1!A1)

where "formula" represents the function I'm looking for - it would use the formula I've entered in Sheet1!A1, but evaluate it with adjusted ranges.

So if I entered =formula(sheet1!A1) in cell A2, it would return the result of =SUM(Sheet1!B2:C2)

...Kind of a formula-based version, instead of range-based, of the "INDIRECT" function

The reason for doing this is sometimes I build complicated formulas which I then duplicate in various locations in the workbook. If I could link back to one cell where the formula is defined, then I could change it only in one place.

I realize that I could accomplish this by simply creating a UDF then using that UDF where I need it - I could then just change the VBA in the UDF.

But I think the method I described above would be a more useful, accessible way to accomplish the same thing.

I'm wondering if the fact that this would have to be evaluated in VBA would make it impossible - would someone have to basically code the equivalent to all the existing worksheet functions in the UDF in order to accomplish this?

Anyway, thanks for any comments you can all provide.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
you could set up the formula as a defined name & refer to that. for example:
Book2
ABCDE
1123
2
3
4235
5
6
Sheet1


...myformula is defined (using insert | name | define) as:

=SUM(OFFSET(INDIRECT("RC",FALSE),0,-1),OFFSET(INDIRECT("RC",FALSE),0,-2))
 
Upvote 0
Here is a function you can call either from VBa or as a worksheet function, it evaluates an input string as a formula (or array formula) and returns the result of the evaluation. You could adapt this to do what you want.

Public Function EVAL(theInput As Variant) As Variant
'
' if UDF evaluate the input string as though it was on this sheet
' else evaluate for activesheet
'
Dim vEval As Variant
Application.Volatile
On Error GoTo funcfail
If not IsEmpty(theInput) then
If TypeOf Application.Caller.Parent Is Worksheet Then
vEval = Application.Caller.Parent.Evaluate(Cstr(theInput))
Else
vEval = Application.Evaluate(cstr(theInput))
End If
If IsError(vEval) Then
EVAL = CVErr(xlErrValue)
Else
EVAL = vEval
End If
End If
Exit Function
funcfail:
EVAL = CVErr(xlErrNA)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top