Excel VBA - if no workbook open add new blank one

Posted on 2014-08-30
Last Modified: 2014-09-01

What Excel VBA code would I use to check if there is a workbook open and if not add a new blank one.

Question by:murbro
    LVL 1

    Accepted Solution

    Probably, You have to at least open one workbook to do VBA coding. So it must have at least one workbook open on Excel. Please write more about your question and need

    So I guess you check whether some xls file workbook open or not, If not, create a blank workbook.
    Is it what you want ? if so, please read the following code

     Sub check_open()
     Dim oldworkbook As String
     Dim newworkbook As String
     oldworkbook = "checkfile.xls"
     'Debug.Print InStr(, oldworkbook)
     If InStr(, oldworkbook) > 0 Then
     'Workbooks.Add.SaveAs Filename:="c:\temp\" & newworkbook
     End If
     End Sub

    Open in new window

    LVL 67

    Assisted Solution

    That's correct - you cannot use VBA code without a workbook. You can, however, run Automation code (e.g. from VB, PowerShell, VBS, ...) outside of Excel. But the way the question is asked, that is out of scope.
    LVL 85

    Assisted Solution

    by:Rory Archibald
    Not really - you can automate a different instance of Excel from Excel after all. Also, it depends if you regard an add-in as a workbook.

    A simple Application.Workbooks.Count statement will tell you how many workbooks are open, including add-ins and hidden workbooks like Personal Macro workbooks.

    Author Closing Comment

    Thank for the help

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Sometimes we don't want to show zeros in our Excel spreadsheets. This is sometimes most evident in our charts. Look at the chart below, all the zero values are visible. I think that all will agree with the fact that zero values are not looking nice …
    Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now