+ Reply to Thread
Results 1 to 5 of 5

Prevent functions from running while macro is running

Hybrid View

  1. #1
    Registered User
    Join Date
    03-23-2009
    Location
    Mountain Creek,Australia
    MS-Off Ver
    Excel 2002
    Posts
    5

    Prevent functions from running while macro is running

    My worksheet contains several custom functions. The functions calculate, as usual, when data is entered. How do I prevent the functions from recalculating while a macro is running? The macro carries out calculations as well, and each time data is entered by the macro the functions recalculate. I have tried
    Application.EnableEvents = False
    and
    Application.Volatile False
    but they do not stop the functions. Are there any suggestions that may help.
    Thankyou
    Peter

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Prevent functions from running while macro is running

    Hi Peter,

    It's not particularly elegant or high-tech, but the following approach might be suitable for preventing your user-defined functions from recalculating while your macro is executing:

    Option Explicit
    
    
    Dim bMacroRunning As Boolean
    
    
    Function MyFunction(iMyInput As Integer) As Integer
    
        If bMacroRunning = True Then Exit Function
    
    '   This is where MyFunction is defined
        MyFunction = iMyInput ^ 2
    
    End Function
    
    
    Sub MyMacro()
    
        Dim i As Integer
    
        bMacroRunning = True
    
    '       This is where all the MyMacro code is entered
            For i = 1 To 10
                MsgBox "This is iteration no " & i
            Next i
    
        bMacroRunning = False
    
    End Sub
    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    03-23-2009
    Location
    Mountain Creek,Australia
    MS-Off Ver
    Excel 2002
    Posts
    5

    Re: Prevent functions from running while macro is running

    Thanks for the reply Greg. I can't make it work so could you give me an explanation of the process
    Thanks
    Peter

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Prevent functions from running while macro is running

    You could set Calc to Manual prior to running the routine, this should not invoke the function at all.

    Public Sub YourMacro()
    Dim xlCalc As XlCalculation
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
    End With
    ...your code
    With Application: .Calculation = xlCalc: End With
    End Sub

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Prevent functions from running while macro is running

    Hi again,

    The statement:
    If bMacroRunning = True Then Exit Function
    must be inserted in EACH of the user-defined functions which you wish to prevent from calculating while the MyMacro routine is executing.

    The variable bMacroRunning is defined as a module-level variable (i.e. at the top of the Code Window) - this means that it is available to all routines contained in the module.

    As soon as the MyMacro routine is executed, the bMacroRunning variable is set to TRUE. Whenever your user-defined functions are called (from the worksheet), the above If statement causes control to exit immediately from the user-defined function.

    When the MyMacro routine has executed, the bMacroRunning variable is set to FALSE - this means that user-defined functions will be calculated, as the result of the above If statement will be FALSE.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1