Ignoring code to shut a workbook if it is already shut

C

Cammy

I have some coding which closes and saves workbooks;
Workbooks("Bund Current Payoff.xls").Close SaveChanges:=True etc

but I want the code not to have an error and have a problem if the file has
already been closed when the code is run.

How do I get around this problem?
 
A

Allllen

Test if the workbook is still open first:

Dim wbTest As Workbook
on error resume next
Set wbTest = Workbooks("Bund Current Payoff.xls")
on error goto 0

If Not wbTest Is Nothing Then Workbooks("Bund Current Payoff.xls").Close
SaveChanges:=True

wbTest will be Nothing if it wasn't already open
 
Top