evaluate formula vs VBA function

df9864

Board Regular
Joined
Sep 28, 2004
Messages
108
I have a complex spreadsheet with an Excel table containing my data and formula [in Excel 2010]
I have a series of columns by month. eg. Jan x, Jan y, Jan z, Jan Total, Feb x, Feb y, Feb z, Feb total, etc (thru to end of year).
I have a YTD formula that is driven by a drop-down (where I select the current month) = eg. Feb z YTD (where the formula needs to pick up Sum(Jan z, Feb z).

From scanning the web, I've plumped for an evaluate formulae. So I have a fixed cell which in my above example, does a vlookup against my selected month to retrieve the formula I need - in my example I return the text (eg, into cell $D$5):
SUM(DATA[@[JAN z]],DATA[@[FEB z]])

In my YTD z column, I have the formula:
= eval($D$5)

with the following code in VBA:

Function eval(r As Range) As Variant
eval = Evaluate(r.Value)
End Function

The formula 'works' but it does not auto-update, and if any of the numbers change, I need to force the update by either hitting return within the cell, or replacing the entire column with the formula.
I have my formula on auto-update but can't figure out how to make this dynamic.
Any ideas from the power users? The only alternative I can think of is write a huge IF statement (IF my current month = Jan, = Jan z, If Feb, Sum(Jan z, Feb z) etc....

Any help would be hugely appreciated.
Cheers
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try:
Code:
[COLOR=#333333]Function eval(r As Range) As Variant
[/COLOR][COLOR=#ff0000]    Application.Volatile[/COLOR]
[COLOR=#333333]    eval = Evaluate(r.Value)[/COLOR]
[COLOR=#333333]End Function[/COLOR]
 
Upvote 0
Excellent - thankyou!
Are there any downsides in having a 'volatile' application? ;)
Is this ultimately a memory hogging way and I might be better off writing the big IF statement, IF(month = Jan, x, If(Month = Feb, y, )) etc?
 
Upvote 0
They should be used sparingly, as they run every time Excel recalculates.
If you find that your worksheet is running too slow, you may want to look at other options. But if performance is fine, I wouldn't worry too much about it. As long as you don't overdo, you should be OK.

Note that some native functions are volatile by nature. Here are some good discussions on the volatility of both native functions and UDFs.
https://fastexcel.wordpress.com/201...10-volatile-functions-and-function-arguments/
Volatile Excel Functions -Decision Models
Handle Volatile Functions like they are dynamite | Chandoo.org - Learn Microsoft Excel Online
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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