Consulting

Results 1 to 7 of 7

Thread: check calculation state on workbook open

  1. #1
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    check calculation state on workbook open

    Hi all,

    I am trying to check (and set to automatic) the calculation state when a workbook is opened.

    I've created a personal macro workbook
    I've written the following sub (which works) which is called by the workbook open event.
    the problem is, when first opening excel, the workbook open event triggers before application.calculation is initialised.

    does anyone have any advice?
    thanks


    Sub CheckCalculationState()
    'read excel calculation state
    'warn user if calculation is not automatic, and offer fix
    
    Dim bIsAuto As Boolean
    
        'ID calculation state
        bIsAuto = False
        If Application.Calculation = xlCalculationAutomatic Then bIsAuto = True
        
        'warn/fix calculation state
        If bIsAuto = False Then
            Select Case MsgBox("xlCalculation is not set to automatic. Do you want to set it to set it to Automatic?", vbYesNo Or vbInformation, "clCalculation State")
    
                Case vbYes
                    Application.Calculation = xlCalculationAutomatic
        
                Case vbNo
                    'do nothing
    
            End Select
        End If
    End Sub
    Remember: it is the second mouse that gets the cheese.....

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    Perhaps use OnTime, scheduling a run of CheckCalculationState at Now() + timevalue("00:00:02")That's a 2 second delay - experiment with different times too maybe?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,639
    If I close Excel and open it again the calcultation is always set to 'automatic'.
    No need to check or alter anything.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Call your sub from
    Workbook_SheetActivate(ByVal Sh As Object)   
       If Not Application.Calculation = xlCalculationAutomatic Then CheckCalculationState
    End sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    Thanks p45Cal

    this might be working.
    and for anyone else reading this, if, for any reason, you save a workbook with calculation turned off, excel remembers and starts to apply this setting to everything you open.
    this can really mess with your head.
    Remember: it is the second mouse that gets the cheese.....

  6. #6
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    mr SamT,

    I think your solution would also work.
    this would prevent the sub from firing until the object was initialised.

    werafa
    Remember: it is the second mouse that gets the cheese.....

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It can be such an issue.

    Since I work with so many "Not quite right" Workbooks from others, I have a sub in my Personal workbook that sets all Persistent functions back to normal and it can be run manually, but it does run every time Personal opens.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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