+ Reply to Thread
Results 1 to 8 of 8

Macro to close files

  1. #1
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Macro to close files

    I created a macro that opens a series of files according to data in another workbook. I now want to create a macro that will sequentially go through them all and close them (with saving). I tried the code below but it keeps getting hung up on close line. Can someone help me with this.

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor wenqq3's Avatar
    Join Date
    04-01-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    868

    Re: Macro to close files

    Please Login or Register  to view this content.
    No sure is this what you need.
    -If the problem is solved, please mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved".

    -Always upload a workbook before start your question
    To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

    +++ If my answer(s) helped you, please add me reputation by click on * +++

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Macro to close files

    Hi, wenqq3,

    I may doubt
    Please Login or Register  to view this content.
    as this will close the workbook with code (and that should stop the macro from running).

    IŽd prefer to set objects and work with these - both for opening and closing the workbooks.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Valued Forum Contributor wenqq3's Avatar
    Join Date
    04-01-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    868

    Re: Macro to close files

    Hi HaHoBe,
    you are correct. Miss that part. Perhaps
    Please Login or Register  to view this content.
    move after end with. so the code can finish running before close.

  5. #5
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Macro to close files

    I tried this code but it comes back with an error. I also need to figure out when these files close with saving of changes that they are changed to a specific directory which has been created by a separate macro that creates a sub directory from a date value in a cell.

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro to close files

    Are you trying to close the workbook that opened all the workbooks or are you wanting to close all the workbooks opened by the original workbook?




    Edit: I need an emoticon for being cross-eyed after reading my response!
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  7. #7
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Macro to close files

    Sorry Mordred, I should have been more specific. I want to close the files that are opened by the macro in the main workbook and then close them (again with saving to a specific directory).

  8. #8
    Registered User
    Join Date
    02-13-2013
    Location
    Bergen County
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Macro to close files

    Hi agpyle,

    I'm assuming that the files that are being opened are also excel workbooks If so try this code.
    This will close all open workbooks except the workbook where you ran the original macro from.

    Sub CloseAllWorkbooks()
    Dim MyBook As Workbook
    For Each MyBook In Workbooks
    If MyBook.Name <> ThisWorkbook.Name Then
    MyBook.Close savechanges:=True
    End If
    Next MyBook
    ThisWorkbook.Close savechanges:=True
    End Sub

+ 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