ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Close almost all workbooks (https://www.excelbanter.com/excel-programming/415328-close-almost-all-workbooks.html)

ChrisP

Close almost all workbooks
 
I've written some VBA code to open one workbook (call it "A") and in the open
event for workbook A I have it coded to open various other workbooks ("B",
"C", "D", etc).

I'm doing it this way so all the links are updated (one is password
protected and I can't give out the password so I've had to code opening them
in VBA and lock the VBA code).

I have everything I need except how to close workbooks B, C, D, etc. I don't
want to close all the workbooks as users may have other excel workbooks open
at the time. Any help is appreciated!

Thanks,
Chris

dustinbrearton via OfficeKB.com

Close almost all workbooks
 
You could use a for each workbook loop and then put an if statement in the
for each that says if the name is not "A" then close workbook.




ChrisP wrote:
I've written some VBA code to open one workbook (call it "A") and in the open
event for workbook A I have it coded to open various other workbooks ("B",
"C", "D", etc).

I'm doing it this way so all the links are updated (one is password
protected and I can't give out the password so I've had to code opening them
in VBA and lock the VBA code).

I have everything I need except how to close workbooks B, C, D, etc. I don't
want to close all the workbooks as users may have other excel workbooks open
at the time. Any help is appreciated!

Thanks,
Chris


--
Message posted via http://www.officekb.com


Per Jessen

Close almost all workbooks
 
Hi Chris

Look at this:

Set wbB = Workbooks.Open(Filename:="Book1.xls") ' Open "B"
'Your code
'More code
wbB.Close ' Close "B"

Hopes it helps

Best regards,
Per
"ChrisP" skrev i meddelelsen
...
I've written some VBA code to open one workbook (call it "A") and in the
open
event for workbook A I have it coded to open various other workbooks ("B",
"C", "D", etc).

I'm doing it this way so all the links are updated (one is password
protected and I can't give out the password so I've had to code opening
them
in VBA and lock the VBA code).

I have everything I need except how to close workbooks B, C, D, etc. I
don't
want to close all the workbooks as users may have other excel workbooks
open
at the time. Any help is appreciated!

Thanks,
Chris



ChrisP

Close almost all workbooks
 
Thanks! One more question, workbooks B & C I don't want to save but D I do.
How would I write that?

Thanks again!
Chris

"Per Jessen" wrote:

Hi Chris

Look at this:

Set wbB = Workbooks.Open(Filename:="Book1.xls") ' Open "B"
'Your code
'More code
wbB.Close ' Close "B"

Hopes it helps

Best regards,
Per
"ChrisP" skrev i meddelelsen
...
I've written some VBA code to open one workbook (call it "A") and in the
open
event for workbook A I have it coded to open various other workbooks ("B",
"C", "D", etc).

I'm doing it this way so all the links are updated (one is password
protected and I can't give out the password so I've had to code opening
them
in VBA and lock the VBA code).

I have everything I need except how to close workbooks B, C, D, etc. I
don't
want to close all the workbooks as users may have other excel workbooks
open
at the time. Any help is appreciated!

Thanks,
Chris




dustinbrearton via OfficeKB.com

Close almost all workbooks
 
Try this code

Sub CloseWorkbooks()

Dim WB As Workbook

For Each WB In Application.Workbooks
If WB.Name < "A.xls" Then
If WB.Name = "D.xls" Then
WB.Close SaveChanges:=True
Else
WB.Close SaveChanges:=False
End If
End If
Next WB


End Sub



ChrisP wrote:
Thanks! One more question, workbooks B & C I don't want to save but D I do.
How would I write that?

Thanks again!
Chris

Hi Chris

[quoted text clipped - 27 lines]
Thanks,
Chris


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200808/1



All times are GMT +1. The time now is 06:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com