• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

Excel VBA - if no workbook open add new blank one

Hi

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

Thanks
0
Murray Brown
Asked:
Murray Brown
3 Solutions
 
rwniceingCommented:
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
newworkbook="testing.xls"
 oldworkbook = "checkfile.xls"
 'Debug.Print InStr(ActiveWorkbook.name, oldworkbook)
 If InStr(ActiveWorkbook.name, oldworkbook) > 0 Then
 Debug.Print ActiveWorkbook.name
 Else
 Workbooks.Add
 'Workbooks.Add.SaveAs Filename:="c:\temp\" & newworkbook
 End If
 End Sub

Open in new window

0
 
QlemoC++ DeveloperCommented:
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.
0
 
Rory ArchibaldCommented:
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.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank for the help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now