You could simply:

On Error Resume Next
Workbooks("File1.xls").Close
Workbooks("File2.xls").Close
On Error Goto 0

Paul


----- Original Message ----
> From: 0 1 <hhholme...@gmail.com>
> To: MS EXCEL AND VBA MACROS <excel-macros@googlegroups.com>
> Sent: Wed, December 15, 2010 10:36:02 AM
> Subject: Re: $$Excel-Macros$$ Check if two files are open; if not, open them
> 
> Using two separate if-then statements did the trick. But now I have a
> related problem.
> 
> I'm trying to reverse the code in order to *close* the sheets, if they
> are open. I figured I could use the same code for the Function
> WorkbookOpen ..., but a new subroutine for closing them:
> 
> Public Sub CloseSourceDocs()
> 
>     If Not WorkbookOpen("File1.xls") Then
>         Workbooks("File1.xls").Activate
>         ActiveWorkbook.Save
>         ActiveWorkbook.Close
>     End If
> 
>     If Not WorkbookOpen("File2.xls") Then
>         Workbooks("File2.xls").Activate
>         ActiveWorkbook.Save
>         ActiveWorkbook.Close
>     End If
> 
> End Sub
> 
> ###
> 
> If *both* files are opened, the above code works fine. (And I don't
> know why - if "If Not WorkbookOpen" = True, then the sheet is *not*
> open and the code should be skipped.)
> 
> If either or both files are closed, I get:
> 
> "Subscript out of range" error
> 
> ... and it points to the Workbooks ... Active statement (of the
> offending sheet(s)).
> 
> Any ideas?
> 
> -- 
>----------------------------------------------------------------------------------
>-
> Some important links for excel users:
> 1. Follow us on TWITTER for tips tricks and links : 
>http://twitter.com/exceldailytip
> 2. Join our LinkedIN group @ http://www.linkedin.com/groups?gid=1871310
> 3. Excel tutorials at http://www.excel-macros.blogspot.com
> 4. Learn VBA Macros at http://www.quickvba.blogspot.com
> 5. Excel Tips and Tricks at http://exceldailytip.blogspot.com
> 
> To post to this group, send email to excel-macros@googlegroups.com
> 
> <><><><><><><><><><><><><><><><><><><><><><>
> Like our page on facebook , Just follow below link
> http://www.facebook.com/pages/discussexcelcom/160307843985936?v=wall&ref=ts
> 

-- 
----------------------------------------------------------------------------------
Some important links for excel users:
1. Follow us on TWITTER for tips tricks and links : 
http://twitter.com/exceldailytip
2. Join our LinkedIN group @ http://www.linkedin.com/groups?gid=1871310
3. Excel tutorials at http://www.excel-macros.blogspot.com
4. Learn VBA Macros at http://www.quickvba.blogspot.com
5. Excel Tips and Tricks at http://exceldailytip.blogspot.com
 
To post to this group, send email to excel-macros@googlegroups.com

<><><><><><><><><><><><><><><><><><><><><><>
Like our page on facebook , Just follow below link
http://www.facebook.com/pages/discussexcelcom/160307843985936?v=wall&ref=ts

Reply via email to