Results 1 to 10 of 21

Thread: Close All Open Excel Files With VBA Open Close File issue unsolved

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    VBA To Close All Other Workbooks Except Active One

    vba is placed in a seperate file 1.xlsm
    now what i need
    there can be 2-3 or it can be 4 files opened along with this 1.xlsm
    i will be in 1.xlsm file & i will ran the macro
    now what i need is when i ran the macro it should go to next open file and use ALT+ F4(this is shorcut key to close the excel file) and that will be close and then again go to next open file and use ALT+ F4 and again if there is a file then do the same do till all files are closed except 1.xlsm
    and i wanted to inform u sir by doing ALT+ F4 only my file is closing and i want to close all the file by this way only sir
    So plz have a look into this problem and help me in solving this problem
    we have to solve this problem only by vba only sir so plz help sir & mam

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Try this

    Code:
    Sub CloseAllOtherWorkbooks()
    '
    ' CloseAllOtherWorkbooks Macro
    ' Keep this workbook open and close all other workbooks in this instance
    '
    ' Keyboard Shortcut: Ctrl+q
    '
        Dim wbk As Workbook
        Dim strBookNames() As String
        Dim lngBooksCount As Long
        
        For Each wbk In Application.Workbooks
            If wbk.Name <> ThisWorkbook.Name Then
                lngBooksCount = lngBooksCount + 1
                ReDim Preserve strBookNames(1 To lngBooksCount)
                strBookNames(lngBooksCount) = wbk.Name
            End If
        Next wbk
        For lngBooksCount = 1 To lngBooksCount
            'Change to True if the files have to be saved before closing
            Workbooks(strBookNames(lngBooksCount)).Close False
        Next lngBooksCount
        Set wbk = Nothing
        Erase strBookNames
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Thnx ExcelFox for helping me in solving this problem
    Actually i downloaded the file and i open the macro file and i ran it but its not working i tried many code to close the file but its not closing
    when i close the file and then i open then it is closing the code is perfect no doubt
    i learned that once i download the file and it opens by default then macro will not work until its close and reopen again
    i searched on google there was alt+F4 which is closing that file but i think if we use that also in the vba code then it will also not work i will manage it

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Not sure I understand that. So you're saying the code I sent works, right?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  5. #5
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    No doubt ur code is perfect sir

Similar Threads

  1. Replies: 1
    Last Post: 02-19-2015, 03:49 AM
  2. Replies: 4
    Last Post: 04-10-2014, 10:58 PM
  3. Replies: 6
    Last Post: 09-07-2013, 03:40 PM
  4. Replies: 15
    Last Post: 08-23-2013, 12:03 PM
  5. Replies: 4
    Last Post: 06-09-2013, 01:43 AM

Tags for this Thread

Posting Permissions

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