![]() |
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!!! |
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!!! |
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 |
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!!! |
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!!! |
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!!! |
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!!! |
Closing Within Macros
Okay, here is the process. Import data into a sheet, it has the original
imported file name. From there a bunch of changes are made, then a filter is applied. The Visible filtered data is then copy and pasted into a new doc Book1, from there another filter is applied, the visible data is copied and pasted into Book2. From there some more changes are made, and Book2 is the final product. So I want to close A: The Original wkbook with the original filename, B: Book1. Personal is where all my macros are stored, so I'm wanting that to stay open as well as Book2. Make sense? And thus far I have been unable to get any of your formulas to work. Most likely because I'm not so skilled :) But you're being super helpful. Thanks!!! "Tom Ogilvy" wrote: 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!!! |
Closing Within Macros
ActiveWindow.Visible = False
For Each bk In Application.Workbooks If bk.Windows(1).Visible Then MsgBox bk.Name If bk.Name < ThisWorkbook.Name Then bk.Close SaveChanges:=False End If End If Next Okay, I added this to the top of the formula that you gave me, and it's working great, shutting everything visible down, effectively leaving Book2 which I made hidden to avoid being closed. BUT, when ever I try to add the line to make the Book2 Visible again, it locks up, or doesn't work. This works to close down everything I'm not using, I just made the Personal wkbook hidden, and it worked great. Now can I take out the message box thing and will it still close down the stuff? Thanks again for all your help... :) "Tom Ogilvy" wrote: 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!!! |
Closing Within Macros
Since you stated the code is contained in Personal.xls
ActiveWindow.Visible = False For Each bk In Application.Workbooks If bk.Windows(1).Visible Then If bk.Name < ThisWorkbook.Name Then bk.Close SaveChanges:=False End If End If Next for each bk in Application.Workbooks if bk.Name < ThisWorkbook.Name then bk.Windows(1).Visible = True end if Next My approach was based on almost no knowledge of your situation. Based on your description, you should be able to maintain a reference to the workbook you want to retain and just close down all the others which you would also maintain references to Some pseudo code that might reflect how you process flows. If you create you workbooks with Workbooks.Add rather than copying a sheet, you could use the same approach by setting a reference to the workbook after you added it. Dim bk1 as Workbook, bk2 as Workbook Dim bk3 as Workbook set bk1 = Workbooks.Open("C:\blah.xls") bk1.Worksheets(1).Copy set bk2 = ActiveWorkbook ' work with bk2 bk2.worksheets(1).copy set bk3 = ActiveWorkbook ' now close bk1 and bk2 bk1.Close SaveChanges:=False bk2.Close SaveChanges:=False -- Regards, Tom Ogilvy "bodhisatvaofboogie" wrote in message ... ActiveWindow.Visible = False For Each bk In Application.Workbooks If bk.Windows(1).Visible Then MsgBox bk.Name If bk.Name < ThisWorkbook.Name Then bk.Close SaveChanges:=False End If End If Next Okay, I added this to the top of the formula that you gave me, and it's working great, shutting everything visible down, effectively leaving Book2 which I made hidden to avoid being closed. BUT, when ever I try to add the line to make the Book2 Visible again, it locks up, or doesn't work. This works to close down everything I'm not using, I just made the Personal wkbook hidden, and it worked great. Now can I take out the message box thing and will it still close down the stuff? Thanks again for all your help... :) "Tom Ogilvy" wrote: 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!!! |
All times are GMT +1. The time now is 12:05 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com