Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi all,
I need a macro that checks for other open workbooks and, if they are there, closes them (without saving). I'm using Workbook_Activate so that this check will be done anytime a user changes to another workbook and back again. Here's my code: Private Sub Workbook_Activate() Dim i, NumOpen As Integer Dim w As Workbook NumOpen = Application.Workbooks.Count MsgBox "There are " & NumOpen & " workbooks open" If NumOpen 1 Then For Each w In Workbooks If (w.Name < ThisWorkbook.Name And w.Name < "PERSONAL.XLS") Then MsgBox w.Name & " will be closed" Application.Workbooks(w.Name).Close savechanges:=False End If Next w End If End Sub This all works just dandy, except that if a workbook gets closed Excel crashes. If there are no other open workbooks, there is no problem. The troublesome line of code is: Application.Workbooks(w.Name).Close savechanges:=False I've tried several variations on that line which all result in the same problem (e.g. just using w.close). It does close the other workbook, but then it crashes. What seems to happen is that it goes through the loop, closes the workbook, and then exits the sub but then restarts the sub and that causes the crash. Any help would be appreciated. Thanks, Tim |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Closing workbooks w/o closing Excel | Excel Discussion (Misc queries) | |||
closing multi workbooks | Excel Discussion (Misc queries) | |||
Closing workbooks | Excel Programming | |||
Opening and Closing workbooks | Excel Programming | |||
Closing workbooks through macro | Excel Programming |