Consulting

Results 1 to 9 of 9

Thread: Prevent Function calls in worksheet when running macro

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    34
    Location

    Prevent Function calls in worksheet when running macro

    I have a bunch of functions in a worksheet ( =MyFunc(A1) ) and when I am stepping through my macro in that same spreadsheet it keeps jumping back and running through the function calls. This is annoying and clearly adding overhead to my script. How should I stop this?
    I was thinking of eliminating the live function calls in the spreadsheet and just writing the data in one time.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Turn calculation off, and reset afterwards.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2007
    Posts
    34
    Location
    thanks. Created a function to turn calculations and screenupdating on/off
    Function CalcSet(x As Boolean)
    If x Then
        With Application
            .ScreenUpdating = True 'Turns on screen updating
            .Calculation = xlCalculationAutomatic 'prevents function calls in spreadsheet during macro
        End With
    Else
        With Application
            .ScreenUpdating = False 'Turns off screen updating
            .Calculation = xlCalculationManual 'prevents function calls in spreadsheet during macro
        End With
    End If
    End Function

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Remember to add error handling to call CalcSet and avoid other problems.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Oct 2007
    Posts
    34
    Location
    you mean, On Error Goto 0?

    I haven't done much error handling, so far I've wanted the errors so I know where the code screwed up.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Call calcset(True)
    On Error GoTo Exits
    'Do your stuff
    Exits:
    Call calcset(False)
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I use a function to record the state of the calculation before I start, and reset it to that when I'm done. (There are sometimes where I'm working in manual mode and want to keep it like that.)

    Dim xlCalcState As Long
    Public Sub Environ_RestoreSettings()
    'Macro created 06/11/2005 22:33 by Ken Puls
    'Macro Purpose: To restore application properties to user settings
    'Restore screen updates, clear statusbar and set
    'calculation back to user's initial setting
        With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .Calculation = xlCalcState
        .StatusBar = False
        End With
        'Set the calculation state variable to 0
        xlCalcState = 0
    End Sub
    
    Public Sub Environ_SpeedBooster()
    'Macro created 06/11/2005 22:29 by Ken Puls
    'Macro Purpose:  To set application properties to maximize speed
        With Application
        'Add workbook if necessary to avoid calculation error messages
        If .Workbooks.Count = 0 Then .Workbooks.Add
        'Record current calculation state
        xlCalcState = Application.Calculation
        'Turn off screen updates and set calculation to manual
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
        End With
    End Sub
    Then when running in your macro:
    On Error GoTo ErrHandler
    'Do whatever your macro would normally do
    Exit Sub
    ErrHandler:
    Call Environ_RestoreSettings
    End Sub
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Ken Puls
    I use a function to record the state of the calculation before I start, and reset it to that when I'm done. (There are sometimes where I'm working in manual mode and want to keep it like that.)
    I do something similar, but I actually found a use fir the Type construct here

    http://tinyurl.com/2fj628z
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Ah... nice stuff there, Bob.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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