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
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.