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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,536
Office Version
  1. 365
Platform
  1. Windows
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]
 

df9864

Board Regular
Joined
Sep 28, 2004
Messages
108
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?
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,536
Office Version
  1. 365
Platform
  1. Windows
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:

Forum statistics

Threads
1,144,630
Messages
5,725,401
Members
422,623
Latest member
Dave52

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
Top