• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Volatile User Defined Functions

You are here: Home / Excel VBA / Volatile User Defined Functions
volatile user defined functions
April 13, 2018 by Philip Treacy

Excel has the concept of volatile and non-volatile functions.

Microsoft define a volatile function as one that is recalculated every time calculation occurs in any cell in the worksheet. There's actually a bit more to it than that but for the sake of keeping it simple we'll go with that definition.

A non-volatile function is one that only recalculates when its inputs (arguments) change.

NOW() is a volatile function. Every time the sheet recalculates, NOW() is executed and gives you the current date and time. So when you change any cell on the sheet, NOW() is executed.

SUM() is not volatile. If we write

    =sum(A1:A5)

This will only be recalculated if its arguments, any cell in the range A1 to A5, changes.

Volatile User Defined Functions

By default, a UDF is non-volatile, but you can use the Application.Volatile method to make it volatile.

However, making a UDF volatile is to be avoided, and it's well known that UDF's execute more slowly than built in functions, so having lots of volatile UDF's in your workbook could slow down your Excel noticeably.

If you want to use the Application.Volatile method, ask yourself if that is really necessary. Let's look at an example.

If I write this function into C1

 
Function AddUp(MyCell As Range)

    AddUp = MyCell.Value + Range("A1").Value

End Function

and pass in B1 as the argument, the function will only execute when the argument, cell B1, changes. But this isn't what we want, we want the sum of A1 and B1.

not passing arguments correctly to udf

Excel watches the arguments to functions and if they change, that function is marked as requiring a recalculation so its output can be refreshed on the sheet.

Rather than using the Application.Volatile method in this function, we should rewrite it so that all cells we are using in the calculation are passed in as arguments


Function AddUp(MyRange As Range)

    AddUp = WorksheetFunction.Sum(MyRange)

End Function

passing a range to udf

NOTE : I know this function is just replicating the SUM function, but this is just a demonstration of how you should pass all cells you want included in your calculation as arguments to your UDF.

When we change a cell not passed into our UDF, the AddUp function is not executed. You can double check this by adding a Debug.Print line to it and watching the Immediate Window in the VBA editor. You will only see the current date and time when the function is executed.


Function AddUp(MyRange As Range)

    AddUp = WorksheetFunction.Sum(MyRange)
    Debug.Print Now()

End Function

watching immediate window in VBA editor

When a Non-Volatile Function is Volatile

Now we get to the 'interesting' part. There are some actions that make non-volatile UDF's, which means all UDF's, volatile.

Despite what Microsoft say, there are times when a UDF is executed even though its arguments are not changed. Even if you use Application.Volatile(False) to explicitly mark your UDF as non-volatile, it won't matter.

Like a number of 'features' in Excel, this problem has been around for years, and has been written about before. I noticed some great comments by Jeff Weir on Charles Williams blog.

In my testing I found that these actions made the UDF recalculate:

  • Deleting a column, row or cell
  • Deleting a sheet
  • CTRL+ALT+SHIFT+F9 - Workbook tree rebuild and a forced recalculation
  • CTRL+ALT+F9 - All open workbook tree rebuild and a forced recalculation

That may not be all the scenarios that cause a von-volatile function to execute, just the ones I've come across so far.

I don't know why Microsoft continue to claim that UDF's are non-volatile when this issue remains. If you've written some UDF's, and are calling them a lot, this can have very detrimental effects on the speed at which your Excel works.

The bottom line is, use UDF's sparingly.

volatile user defined functions

More Excel VBA Posts

Display All Matches from Search in Userform ListBox

Display All Matches from Search in Userform ListBox

Search a range for all partial and full matches of a string, and display matching records (entire rows) in a userform listbox. Sample code and userform.
animating excel charts

Animating Excel Charts

Use animation correctly to enhance the story your data is telling. Don't animate your chart just for some eye candy. Sample code and workbook to download.
dynamic data validation lists in userforms

Dynamic Data Validation Lists in Userforms

Data validation lists using the same source that are dynamically modified to prevent the same choice being made in each list.
show report filter pages for power pivot pivottables

Show Report Filter Pages for Power Pivot PivotTables

PivotTables created from Power Pivot can't use the 'Show Report Filter Pages' option. But this piece of VBA allows you to do just that.
charting real time data in excel

Charting Real Time Data in Excel

Receive data in real time and chart the data as it arrives. Can be used to chart things like stock prices or sensor readings. Sample code and workbook
select multiple items from drop down data validation list

Select Multiple Items from Drop Down (Data Validation) List

Choose multiple items from a data validation (drop down) list and store them all in the same cell. Sample workbook with working VBA.
Excel Calendar (Date Picker) to Use in Worksheets and Userforms

Multi-Language Excel Calendar (Date Picker) for Worksheets and Userforms

Easy to use, highly customizable and multi-language. This date picker is implemented as a userform that is simple to integrate into your workbook.
automating and emailing pivot table reports

Automating and Emailing Pivot Table Reports

Automate the creation and distribution of pivot table reports with some VBA. Send reports to multiple email recipients using Outlook.
search for data with userform

Searching for Data With a User Form

Search a list of records (like a table) using a user form, and then populate the fields of the search form when the record is found.
Checking values in range objects with vba

Checking Values in Range Objects With VBA

Use built in tools or your own code to examine the values contained within Range objects in VBA. Sample code to download.


Category: Excel VBA
Previous Post:Relative Named Ranges
Next Post:Timer or Stopwatch in Excel VBAtimer stopwatch in excel vba

Reader Interactions

Comments

  1. Ethan Strauss

    July 3, 2018 at 2:17 am

    I have a UDF which is not always updating when its arguments are changed. It is kind of a beast of a UDF, is applied to many cells (about 60,000 so far), and is quite slow (7-10 minutes to recalculate the workbook). I have the workbook set to manual calc, but when I hit calculate now, there are frequently some cells which don’t update. I have not figured out a pattern to when cells or do not calculate.
    Note that the speed is an issue, but not really that large of an issue. Having unrecalculated (i.e. incorrect) cells is a BIG issue for me.
    Any suggestions would be appreciated!

    Reply
    • Catalin Bombea

      July 4, 2018 at 11:12 pm

      Hi Ethan,
      Hard to tell without testing the file.
      Instead of using Calculate Now, try forcing a full recalculation, with Ctrl+Alt+Shift+F9.
      Catalin

      Reply
  2. Alex B

    April 20, 2018 at 10:40 pm

    Thanks Phil, I am a lot clearer on when UDFs update after reading your article although like Jeff I am likely to continue to avoid them if possible.

    Reply
    • Mynda Treacy

      April 22, 2018 at 3:24 pm

      For sure Alex.

      Reply
  3. Jeff Weir

    April 18, 2018 at 1:04 pm

    Great article. Because of these volatility issues, I steer clear of UDFs in favour of Excel formula combinations or PowerQuery whenever I can. Which is 100% of the time, pretty much.

    You can read more on my experiences and see an example at the question I posted at StackOverflow some years back: https://stackoverflow.com/questions/29815150/need-to-stop-udfs-recalculating-when-unrelated-cells-deleted

    Reply
    • Philip Treacy

      April 18, 2018 at 9:02 pm

      Thanks Jeff 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Current ye@r *

Leave this field empty

Sidebar

More results...

Featured Content

  • 10 Common Excel Mistakes to Avoid
  • Top Excel Functions for Data Analysts
  • Secrets to Building Excel Dashboards in Less Than 15 Minutes
  • Pro Excel Formula Writing Tips
  • Hidden Excel Double-Click Shortcuts
  • Top 10 Intermediate Excel Functions
  • 5 Pro Excel Dashboard Design Tips
  • 5 Excel SUM Function Tricks
  • 239 Excel Keyboard Shortcuts

100 Excel Tips and Tricks eBook

Download Free Tips & Tricks

Subscribe to Our Newsletter

Receive weekly tutorials on Excel, Power Query, Power Pivot, Power BI and More.

We respect your email privacy

Guides and Resources

  • Excel Keyboard Shortcuts
  • Excel Functions
  • Excel Formulas
  • Excel Custom Number Formatting
  • ALT Codes
  • Pivot Tables
  • VLOOKUP
  • VBA
  • Excel Userforms
  • Free Downloads

239 Excel Keyboard Shortcuts

Download Free PDF

Free Webinars

Excel Dashboards Webinar

Watch our free webinars and learn to create Interactive Dashboard Reports in Excel or Power BI

Click Here to Watch Now

mynda treacy microsoft mvpHi, I'm Mynda Treacy and I run MOTH with my husband, Phil. Through our blog, webinars, YouTube channel and courses we hope we can help you learn Excel, Power Pivot and DAX, Power Query, Power BI, and Excel Dashboards.

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.