cristianbuse / VBA-FastExcelUDFs

Excel User Defined Functions (VBA) run faster
MIT License
14 stars 5 forks source link

VBA-FastExcelUDFs

FastExcelUDFs is a VBA Project that allows faster User Defined Function (UDF) calculation.

Having a large number of User Defined Functions (UDFs) can be very slow because of a known bug in Excel that causes the state of the VBE window to be updated for each UDF called. Note that the bug is not present outside of the UDF context e.g. when calculation is triggered from a macro. If VBE is open and UDFs are calculating then the VBE is flickering and a word [Running] can be observed in the caption.

Solution

A call to TriggerFastUDFCalculation method must be placed in all UDFs so that an async call can do the calculation outside of the UDF context.

Newer Excel versions

Although it might seem that the bug is not present (i.e. there is no visible difference between using and not using the solution presented here), the bug is still manifesting if the VBA IDE is opened via Alt+F11 or via Developer ribbon tab/Visual Basic.

Installation

Just import the following 2 code modules in your VBA Project. To avoid copy-paste issues please download zip and then import from there.

If importing the form then please note that the AsyncFormCall.frx file is also needed when importing. Alternatively, you can recreate the form in 3 simple steps: 1) insert a new userforn 2) rename it to AsyncFormCall 3) Paste the following code in the form code module:

Option Explicit

Private m_enableCall As Boolean

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = True
    If m_enableCall Then FastCalculate
End Sub

Public Property Let EnableCall(ByVal newValue As Boolean)
    m_enableCall = newValue
End Property

Usage

In your UDFs use:

LibUDFs.TriggerFastUDFCalculation

For example:

Public Function TEST_UDF(ByVal value As Boolean) As Variant
    Application.Volatile False
    LibUDFs.TriggerFastUDFCalculation
    '
    TEST_UDF = value
End Function

Notes

License

MIT License

Copyright (c) 2019 Ion Cristian Buse

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.