Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Atlanta, GA
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Close Workbook macro (Excel 2003)

    I am using a template that must have no other files open when it is run. I added a line in the template's Auto_Open macro that closes another open file before the template loads. It reads:

    "ActiveWindow.ActivateNext
    ActiveWindow.Close"

    This works great when there is another file or blank workbook open. However, there are times when there is nothing else open and when this happens, the macro wants to close the template.

    Can someone suggest a statement I can put in this macro that takes into account the condition that has no other open workbook?

    Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Close Workbook macro (Excel 2003)

    Try this:

    Dim i As Integer
    With Application.Workbooks
    For i = .Count To 1 Step -1
    Select Case .Item(i).Name
    Case ActiveWorkbook.Name, "Personal.xls"
    ' Ignore
    Case Else
    .Item(i).Close
    End Select
    Next i
    End With

    The code closes all workbooks except the active one and your personal macro workbook Personal.xls.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Atlanta, GA
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close Workbook macro (Excel 2003)

    It works. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •