VBA to Close Unsaved Workbook

dariuzthepole

Board Regular
Joined
Jul 23, 2008
Messages
111
Hi all,

I have a macro that imports an XML file into a specific spreadsheet, e.g. figures.xls. When I import the XML, it firstly creates it in its own unsaved workbook, e.g. Book1. Obviously, if I keep figures.xls open and import multiple XML files, Book1 becomes Book2, Book3, Book4 etc.

I'm struggling to work out the code that will close any unsaved workbook, e.g. Book1, Book2 etc, without closing figures.xls, as technically after I have imported the data via the macro it will need saving itself.

Any information you can provide would be greatly appreciated!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
For instance,

Code:
Workbooks("Book1.xls").Close 0

which is shorthand for:
Code:
Workbooks("Book1.xls").Close savechanges:=False

If you want to be sure, you could also add the Saved property:

Code:
With Workbooks("Book1.xls")
    .Saved = True
    .Close 0
End With
 
Upvote 0
For instance,

Code:
Workbooks("Book1.xls").Close 0

which is shorthand for:
Code:
Workbooks("Book1.xls").Close savechanges:=False

If you want to be sure, you could also add the Saved property:

Code:
With Workbooks("Book1.xls")
    .Saved = True
    .Close 0
End With

Thanks Wigi.

That works for when the unsaved workbook is called Book1, but it may also be called Book2, Book3 etc.

I guess I'm looking for some sort of IF statement saying:
If Workbook Name is not figures.xls
Close
End If

I can't get the syntax for the above correct!

Thanks for your help.
 
Upvote 0
...I'm struggling to work out the code that will close any unsaved workbook, e.g. Book1, Book2 etc, without closing figures.xls

Sorry, presuming the code is running from figures.xls

Rich (BB code):
Sub exampleCloseAll()
Dim wb As Workbook
    
    For Each wb In Workbooks
        If Not wb.FullName = ThisWorkbook.FullName Then wb.Close False
    Next
End Sub
 
Upvote 0
Sorry, presuming the code is running from figures.xls

Rich (BB code):
Sub exampleCloseAll()
Dim wb As Workbook
 
    For Each wb In Workbooks
        If Not wb.FullName = ThisWorkbook.FullName Then wb.Close False
    Next
End Sub

Ideal! This is exactly what I'm looking for - thanks very much.
 
Upvote 0
You could check this the Path property of each workbook.

If it's "" the it's never been saved.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top