Consulting

Results 1 to 5 of 5

Thread: Ensure at least one workbook open

  1. #1
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location

    Ensure at least one workbook open

    Hey guys,

    Just ran into an issue trying to run my code to restore a user's environment settings to what they were before I started my macro. Code is here:

    Option Explicit
    Dim xlCalcState As Long
    
    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 = False
        .Calculation = xlCalcState
        .StatusBar = False
    End With
    'Set the calculation state variable to 0
    xlCalcState = 0
    End Sub
    The code fails on the line that sets the calculation back if there isn't at least one workbook showing in the Excel window. Anyone know some quick code to drop in there to ensure at least one workbook is open? I'd do it, but I don't have time to look for it right now.

    Thanks!
    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!





  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Nevermind!

    Had a brain wave:

    Sub Environ_RestoreSettings()
    'Macro created 06/11/2005 22:33 by Ken Puls
    'Macro Purpose: To restore application properties to user settings
    Dim wb As Workbook
    'Make sure that there is a workbook showing, or calculation code will fail
    Set wb = ActiveWorkbook
    If wb Is Nothing Then Set wb = Workbooks.Add
    'Restore screen updates, clear statusbar and set
    'calculation back to user's initial setting
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = False
        .Calculation = xlCalcState
        .StatusBar = False
    End With
    'Set the calculation state variable to 0
    xlCalcState = 0
    End Sub
    Thanks!
    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!





  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ken,

    You could try Workbooks.Count > 0. Problem is that if you have a hidden workbook such as Personal.xls then that will count, and I don't know if that will throw your code. An alternative is Windows.Count > 1.
    ____________________________________________
    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

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    And don't dim wb, just use a one-liner ..

    If ActiveWorkbook Is Nothing Then Workbooks.Add
    Set your wb in that same line only if you will need to work with it.

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Sweet!

    Thanks to both. Code already deployed, but I'll fix it up going forward.

    That's what being in a hurry does to you, y'know?
    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
  •