Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 718
Default close all other workbooks

I have a workbook that is very large and I would like a simple code that
closes any open workbooks when I open this one. I have quite a few macros in
this workbook and I tried to use the workbooks.close statement but of course
that closes all workbooks. I don't see see anything like Before_Open as a
macro. Can someone please help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default close all other workbooks

Hi Rob,

Try:

'=============
Private Sub Workbook_Open()
Dim WB As Workbook

For Each WB In Application.Workbooks
If WB.Name < Me.Name Then
WB.Close SaveChanges:=True 'Or False?
End If
Next WB
End Sub
'<<=============

This is workbook event code and should be pasted into the workbook's
ThisWorkbook module *not* a standard module or a sheet module:


---
Regards,
Norman



"Rob" wrote in message
...
I have a workbook that is very large and I would like a simple code that
closes any open workbooks when I open this one. I have quite a few macros
in
this workbook and I tried to use the workbooks.close statement but of
course
that closes all workbooks. I don't see see anything like Before_Open as a
macro. Can someone please help.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,173
Default close all other workbooks

Rob

Something like this will do it, suing the workbook_open() event

Private Sub Workbook_Open()
Dim wb As Workbook
Dim ThisWBName As String

ThisWBName = Me.Name
For Each wb In Workbooks
If wb.Name < ThisWBName Then
wb.Close saveChanges:=True
End If
Next wb

End Sub


--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
www.nickhodge.co.uk


"Rob" wrote in message
...
I have a workbook that is very large and I would like a simple code that
closes any open workbooks when I open this one. I have quite a few macros
in
this workbook and I tried to use the workbooks.close statement but of
course
that closes all workbooks. I don't see see anything like Before_Open as a
macro. Can someone please help.



  #4   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 718
Default close all other workbooks

Thanks Norman but I forgot to mention one thing, others will be using this
workbook and saving it under different names so I will not know the names of
the workbooks. Really what I would like to do is that when one opens this
workbook, all other workbooks close. What do you think?

"Norman Jones" wrote:

Hi Rob,

Try:

'=============
Private Sub Workbook_Open()
Dim WB As Workbook

For Each WB In Application.Workbooks
If WB.Name < Me.Name Then
WB.Close SaveChanges:=True 'Or False?
End If
Next WB
End Sub
'<<=============

This is workbook event code and should be pasted into the workbook's
ThisWorkbook module *not* a standard module or a sheet module:


---
Regards,
Norman



"Rob" wrote in message
...
I have a workbook that is very large and I would like a simple code that
closes any open workbooks when I open this one. I have quite a few macros
in
this workbook and I tried to use the workbooks.close statement but of
course
that closes all workbooks. I don't see see anything like Before_Open as a
macro. Can someone please help.




  #5   Report Post  
Posted to microsoft.public.excel.programming
Rob Rob is offline
external usenet poster
 
Posts: 718
Default close all other workbooks

Nick, thanks but I forgot to mention that I won't know the name of the
workbook since there will be many people using this tool and saving it under
different names.

"Nick Hodge" wrote:

Rob

Something like this will do it, suing the workbook_open() event

Private Sub Workbook_Open()
Dim wb As Workbook
Dim ThisWBName As String

ThisWBName = Me.Name
For Each wb In Workbooks
If wb.Name < ThisWBName Then
wb.Close saveChanges:=True
End If
Next wb

End Sub


--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
www.nickhodge.co.uk


"Rob" wrote in message
...
I have a workbook that is very large and I would like a simple code that
closes any open workbooks when I open this one. I have quite a few macros
in
this workbook and I tried to use the workbooks.close statement but of
course
that closes all workbooks. I don't see see anything like Before_Open as a
macro. Can someone please help.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default close all other workbooks

Hi Rob,

The suggested code is independent of the file name.

In the expression;

If WB.Name < Me.Name Then


Me.Name refers to the workbook holding the code.


---
Regards,
Norman


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
why do all excel worksheets/workbooks close when I close one? Penny Excel Discussion (Misc queries) 1 November 29th 06 03:49 AM
VBA Code req to close all workbooks Neil Atkinson Excel Programming 0 September 23rd 05 11:06 AM
Open Close workbooks bbc1 Excel Discussion (Misc queries) 2 August 28th 05 11:24 AM
Why does Excel close all workbooks? JVernon Setting up and Configuration of Excel 1 July 5th 05 10:38 PM
Close all other workbooks but this one? ianripping[_93_] Excel Programming 3 September 2nd 04 10:30 AM


All times are GMT +1. The time now is 02:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"