View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Cammy Cammy is offline
external usenet poster
 
Posts: 31
Default Specific macro help - runtime error

I have a macro that is supposed to save and close open workbooks and
(theoretically) will not produce an error if they are not open. I have posted
the macro below.

I keep getting a periodic error message when I run the macro for many
workbooks where some are already shut (around 12) but if I keep re rerunning
the macro it eventually works completely.

I get the error
Run time error €˜9
Subscript out of range

Can anyone help me, fix this so it works on the first time with no error
message?

Sub SaveAndCloseWorkbooks()

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

On Error Resume Next
Set wbtest = Workbooks("Gilt Current Payoff.xls")
On Error GoTo 0
If Not wbtest Is Nothing Then Workbooks("Gilt Current Payoff.xls").Close
SaveChanges:=True

etc for 12 more different workbooks.

End Sub