Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Closing Within Macros

I am creating a macro to do some pretty wacky filtering. Some of the ways
That I have it doing it is through pasting onto new workbooks. SO, after
I've reached the end result (which it seems to be working nicely) I have two
extra documents open and I wanted to incorporate into my macro to close them
automatically. I went ahead and recorded a macro of closing those two
documents, and this is what it looked like:

Windows("Book1").Activate
ActiveWindow.Close
Windows("Original Filename").Activate
ActiveWindow.Close

Which effectively leaves me on Book 2 with the finished product. SO, I just
want everything else to close. That above formula would work nicely on the
file it was recorded for, BUT, when I go to use this macro on new documents
and data, it won't work the same because the original filename will be
different. Any ideas??? Thanks!!!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Closing Within Macros

for each bk in application.Workbooks
if bk.Windows(1).Visible then
if bk.Name < "Name of book not to close" then
bk.close SaveChanges:=False
end if
end if
Next

--
Regards,
Tom Ogilvy


"bodhisatvaofboogie" wrote:

I am creating a macro to do some pretty wacky filtering. Some of the ways
That I have it doing it is through pasting onto new workbooks. SO, after
I've reached the end result (which it seems to be working nicely) I have two
extra documents open and I wanted to incorporate into my macro to close them
automatically. I went ahead and recorded a macro of closing those two
documents, and this is what it looked like:

Windows("Book1").Activate
ActiveWindow.Close
Windows("Original Filename").Activate
ActiveWindow.Close

Which effectively leaves me on Book 2 with the finished product. SO, I just
want everything else to close. That above formula would work nicely on the
file it was recorded for, BUT, when I go to use this macro on new documents
and data, it won't work the same because the original filename will be
different. Any ideas??? Thanks!!!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Closing Within Macros

hmmm...it looks to be trying to work, but I can't seem to get it to do what
it is supposed to. Essentially nothing is happening though. No errors are
popping up. Maybe I did something wrong. Break it down simply for me
please!!! :)

"Tom Ogilvy" wrote:

for each bk in application.Workbooks
if bk.Windows(1).Visible then
if bk.Name < "Name of book not to close" then
bk.close SaveChanges:=False
end if
end if
Next

--
Regards,
Tom Ogilvy


"bodhisatvaofboogie" wrote:

I am creating a macro to do some pretty wacky filtering. Some of the ways
That I have it doing it is through pasting onto new workbooks. SO, after
I've reached the end result (which it seems to be working nicely) I have two
extra documents open and I wanted to incorporate into my macro to close them
automatically. I went ahead and recorded a macro of closing those two
documents, and this is what it looked like:

Windows("Book1").Activate
ActiveWindow.Close
Windows("Original Filename").Activate
ActiveWindow.Close

Which effectively leaves me on Book 2 with the finished product. SO, I just
want everything else to close. That above formula would work nicely on the
file it was recorded for, BUT, when I go to use this macro on new documents
and data, it won't work the same because the original filename will be
different. Any ideas??? Thanks!!!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Closing Within Macros

The assumption is that you know the name of the workbook you want to keep
open.

Assume it is the workbook running the code:

' loop through all the workbooks that are open
for each bk in application.Workbooks
' restrict your actions to workbooks that are visible
' so you don't close personal.xls as an example
if bk.Windows(1).Visible then
msgbox bk.name
' Check if this workbook is not one you don't want to close
if bk.Name < ThisWorkbook.Name then
' close the workbook
bk.close SaveChanges:=False
end if
end if
Next

If the workbook running the code is one that will be closed, then you would
have to close it after closing others - otherwise the macro will halt when
you close it:

Dim bk as Workbook, bk1 as Workbook
set bk1 = Workbooks("book to remain open")
loop through all the workbooks that are open
for each bk in application.Workbooks
' restrict your actions to workbooks that are visible
' so you don't close personal.xls as an example
if bk.Windows(1).Visible then
msgbox bk.name
' Check if this workbook is not one you don't want to close
if bk.Name < ThisWorkbook.Name and _
bk.Name < bk1.Name then
' close the workbook
bk.close SaveChanges:=False
end if
end if
Next
thisworkbook.Close SaveChanges:=False

--
Regards,
Tom Ogilvy



"bodhisatvaofboogie" wrote:

hmmm...it looks to be trying to work, but I can't seem to get it to do what
it is supposed to. Essentially nothing is happening though. No errors are
popping up. Maybe I did something wrong. Break it down simply for me
please!!! :)

"Tom Ogilvy" wrote:

for each bk in application.Workbooks
if bk.Windows(1).Visible then
if bk.Name < "Name of book not to close" then
bk.close SaveChanges:=False
end if
end if
Next

--
Regards,
Tom Ogilvy


"bodhisatvaofboogie" wrote:

I am creating a macro to do some pretty wacky filtering. Some of the ways
That I have it doing it is through pasting onto new workbooks. SO, after
I've reached the end result (which it seems to be working nicely) I have two
extra documents open and I wanted to incorporate into my macro to close them
automatically. I went ahead and recorded a macro of closing those two
documents, and this is what it looked like:

Windows("Book1").Activate
ActiveWindow.Close
Windows("Original Filename").Activate
ActiveWindow.Close

Which effectively leaves me on Book 2 with the finished product. SO, I just
want everything else to close. That above formula would work nicely on the
file it was recorded for, BUT, when I go to use this macro on new documents
and data, it won't work the same because the original filename will be
different. Any ideas??? Thanks!!!


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Closing Within Macros

What if the windows I want closed are NOT visible?
I think that may be the issue. the two extras were just for filtering, and
I dont' know if it is recognizing them as visible.

PERSONAL is closing down, the others are remianing open.

All in All there are four open, PERSONAL, Orginal Imported File name, Book1,
Book2. Book 2 is the keeper, and PERSONAL is where the macro is being run,
so I want to close down Book1, and the Original Imported File Name. But I am
thinkin that they are not visible, hence they are not closing. whaddya
think? :)



"Tom Ogilvy" wrote:

The assumption is that you know the name of the workbook you want to keep
open.

Assume it is the workbook running the code:

' loop through all the workbooks that are open
for each bk in application.Workbooks
' restrict your actions to workbooks that are visible
' so you don't close personal.xls as an example
if bk.Windows(1).Visible then
msgbox bk.name
' Check if this workbook is not one you don't want to close
if bk.Name < ThisWorkbook.Name then
' close the workbook
bk.close SaveChanges:=False
end if
end if
Next

If the workbook running the code is one that will be closed, then you would
have to close it after closing others - otherwise the macro will halt when
you close it:

Dim bk as Workbook, bk1 as Workbook
set bk1 = Workbooks("book to remain open")
loop through all the workbooks that are open
for each bk in application.Workbooks
' restrict your actions to workbooks that are visible
' so you don't close personal.xls as an example
if bk.Windows(1).Visible then
msgbox bk.name
' Check if this workbook is not one you don't want to close
if bk.Name < ThisWorkbook.Name and _
bk.Name < bk1.Name then
' close the workbook
bk.close SaveChanges:=False
end if
end if
Next
thisworkbook.Close SaveChanges:=False

--
Regards,
Tom Ogilvy



"bodhisatvaofboogie" wrote:

hmmm...it looks to be trying to work, but I can't seem to get it to do what
it is supposed to. Essentially nothing is happening though. No errors are
popping up. Maybe I did something wrong. Break it down simply for me
please!!! :)

"Tom Ogilvy" wrote:

for each bk in application.Workbooks
if bk.Windows(1).Visible then
if bk.Name < "Name of book not to close" then
bk.close SaveChanges:=False
end if
end if
Next

--
Regards,
Tom Ogilvy


"bodhisatvaofboogie" wrote:

I am creating a macro to do some pretty wacky filtering. Some of the ways
That I have it doing it is through pasting onto new workbooks. SO, after
I've reached the end result (which it seems to be working nicely) I have two
extra documents open and I wanted to incorporate into my macro to close them
automatically. I went ahead and recorded a macro of closing those two
documents, and this is what it looked like:

Windows("Book1").Activate
ActiveWindow.Close
Windows("Original Filename").Activate
ActiveWindow.Close

Which effectively leaves me on Book 2 with the finished product. SO, I just
want everything else to close. That above formula would work nicely on the
file it was recorded for, BUT, when I go to use this macro on new documents
and data, it won't work the same because the original filename will be
different. Any ideas??? Thanks!!!




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Closing Within Macros

what distinguished Book2 from the other books? Let's assume for illustration
purposes that it is the only workbook with just 1 sheet. (personal.xls may
also have only one sheet or it may have multiple sheets)

for each bk in Application.Workbooks
if bk.sheets.count 1 and lcase(bk.name) < _
"personal.xls" then
bk.close SaveChanges:=False
end if
Next

--
Regards,
Tom Ogilvy


"bodhisatvaofboogie" wrote:

What if the windows I want closed are NOT visible?
I think that may be the issue. the two extras were just for filtering, and
I dont' know if it is recognizing them as visible.

PERSONAL is closing down, the others are remianing open.

All in All there are four open, PERSONAL, Orginal Imported File name, Book1,
Book2. Book 2 is the keeper, and PERSONAL is where the macro is being run,
so I want to close down Book1, and the Original Imported File Name. But I am
thinkin that they are not visible, hence they are not closing. whaddya
think? :)



"Tom Ogilvy" wrote:

The assumption is that you know the name of the workbook you want to keep
open.

Assume it is the workbook running the code:

' loop through all the workbooks that are open
for each bk in application.Workbooks
' restrict your actions to workbooks that are visible
' so you don't close personal.xls as an example
if bk.Windows(1).Visible then
msgbox bk.name
' Check if this workbook is not one you don't want to close
if bk.Name < ThisWorkbook.Name then
' close the workbook
bk.close SaveChanges:=False
end if
end if
Next

If the workbook running the code is one that will be closed, then you would
have to close it after closing others - otherwise the macro will halt when
you close it:

Dim bk as Workbook, bk1 as Workbook
set bk1 = Workbooks("book to remain open")
loop through all the workbooks that are open
for each bk in application.Workbooks
' restrict your actions to workbooks that are visible
' so you don't close personal.xls as an example
if bk.Windows(1).Visible then
msgbox bk.name
' Check if this workbook is not one you don't want to close
if bk.Name < ThisWorkbook.Name and _
bk.Name < bk1.Name then
' close the workbook
bk.close SaveChanges:=False
end if
end if
Next
thisworkbook.Close SaveChanges:=False

--
Regards,
Tom Ogilvy



"bodhisatvaofboogie" wrote:

hmmm...it looks to be trying to work, but I can't seem to get it to do what
it is supposed to. Essentially nothing is happening though. No errors are
popping up. Maybe I did something wrong. Break it down simply for me
please!!! :)

"Tom Ogilvy" wrote:

for each bk in application.Workbooks
if bk.Windows(1).Visible then
if bk.Name < "Name of book not to close" then
bk.close SaveChanges:=False
end if
end if
Next

--
Regards,
Tom Ogilvy


"bodhisatvaofboogie" wrote:

I am creating a macro to do some pretty wacky filtering. Some of the ways
That I have it doing it is through pasting onto new workbooks. SO, after
I've reached the end result (which it seems to be working nicely) I have two
extra documents open and I wanted to incorporate into my macro to close them
automatically. I went ahead and recorded a macro of closing those two
documents, and this is what it looked like:

Windows("Book1").Activate
ActiveWindow.Close
Windows("Original Filename").Activate
ActiveWindow.Close

Which effectively leaves me on Book 2 with the finished product. SO, I just
want everything else to close. That above formula would work nicely on the
file it was recorded for, BUT, when I go to use this macro on new documents
and data, it won't work the same because the original filename will be
different. Any ideas??? Thanks!!!


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Closing Within Macros


The ThisWorkbook property returns the workbook of the currentl
executing code. So (assuming the macro is "Original Filename" o
whatever) change:

Windows("Original Filename").Activate
ActiveWindow.Close

to:

thisworkbook.close



Co

--
colofnatur
-----------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...fo&userid=3435
View this thread: http://www.excelforum.com/showthread.php?threadid=54544

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
Closing workbooks w/o closing Excel Barb in MD Excel Discussion (Misc queries) 3 February 15th 10 06:42 PM
Error closing Excel after running series of macros PT_VBA_GRRL[_2_] Excel Programming 1 January 25th 06 09:20 PM
weird saving of a document with macros resulting with macros being transfered to the copy alfonso gonzales Excel Programming 0 December 12th 04 09:19 PM
Closing VB triggers closing Excel Minilek Excel Programming 2 August 6th 04 05:17 PM
closing excel after closing a workbook CWalsh[_2_] Excel Programming 3 January 21st 04 03:33 PM


All times are GMT +1. The time now is 03:35 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"