+ Reply to Thread
Results 1 to 3 of 3

Edit code to close all workbooks except active workbook and other specific workbook

Hybrid View

rocksan Edit code to close all... 09-04-2012, 06:11 AM
JosephP Re: Edit code to close all... 09-04-2012, 07:18 AM
rocksan Re: Edit code to close all... 09-04-2012, 09:29 PM
  1. #1
    Registered User
    Join Date
    04-05-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    88

    Edit code to close all workbooks except active workbook and other specific workbook

    Hello guys, can anyone help me edit this code, currently it closes all workbooks except the active one, I want it to close all except active workbook and another specific workbook.

    I tried editing the code to close active workbook and another specific workbook using array, but it doesnt work.

    Please help out.

    Below is current code

    Function CloseAll()
    
    Dim wkbk As Workbook
      
    Application.ScreenUpdating = False
     
    'On Error GoTo errhandler
    
     For Each wkbk In Application.Workbooks
         If wkbk.Name <> Array(ActiveWorkbook.Name, "OTHERWORKBOOK.xlsm") Then
             wkbk.Close savechanges:=False  'or make it true
         End If
     Next
     
    'errhandler:
    'For Each wkbk In Application.Workbooks
         'If wkbk.Name <> ActiveWorkbook.Name Then
             'wkbk.Close savechanges:=False  'or make it true
         'End If
    'Next
    'Exit Function
    
    'Application.ScreenUpdating = True
    
    End Function
    Thanks all.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Edit code to close all workbooks except active workbook and other specific workbook

    maybe
    Function CloseAll()
    
    Dim wkbk As Workbook
      
    Application.ScreenUpdating = False
     
    'On Error GoTo errhandler
    For Each wkbk In Application.Workbooks
         select case UCase$(wkbk.Name)
             case ucase$(ActiveWorkbook.Name), "OTHERWORKBOOK.XLSM"
             Case else
             wkbk.Close savechanges:=False  'or make it true
         End Select
    Next wkbk
    Application.ScreenUpdating = True
    
    End Function
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    04-05-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Edit code to close all workbooks except active workbook and other specific workbook

    Hi JP,

    Thanks for replying,

    I tried your code, the original code kept the activeworkbook open but closed "OTHERWORKBOOK.XLSM". Apparently the code only takes into account the first workbook only in the line,

    Case UCase$ (ActiveWorkbook.Name), "OTHERWORKBOOK.XLSM"
    So I edited the code like shown below, and now it works.

    Function CloseAll()
    
    Dim wkbk As Workbook
      
    Application.ScreenUpdating = False
     
    For Each wkbk In Application.Workbooks
         Select Case UCase$ (Workbooks(2).Name)
             Case UCase$ (ActiveWorkbook.Name)
             Case Else
             wkbk.Close savechanges:=False      
     End Select
    Next wkbk
    
    End Function
    Thank you very much JP for pointing me in the right direction.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1