+ Reply to Thread
Results 1 to 22 of 22

Close All Open Files in a Specific Folder Using VBA

  1. #1
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Close All Open Files in a Specific Folder Using VBA

    Close All Open Files in a Specific Folder Using VBA

    Dear Forum,

    I need to run a Macro which creates My Report By Opening Several Files from a Specific Folder and then once the Report is Made the same files need to be closed, this Macro worked for a Very Long Time without any hassles however after the Microsoft Office was upgraded from Excel-2010 to Excel-2019 it does not work the way it used to..
    The Macro does the work of generating the Correct Report however when the Opened Files need to be closed a few Files do remain open. this used to not happen earlier however after the migration this issue came up.
    Please find the Code below which is used only for Closing the Open Files in a Specific Folder.

    Please Login or Register  to view this content.
    Can someone please assist me on the above code so that all the Files are closed with this Macro.

    Regards
    e4excel...

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    Ok, this is an out and out guess, and I have no way of testing it. BUT, as you've had quite a few views and no response, let's give it a punt.

    Recent versions of Excel are more aware of the source of files; for example, if they're downloaded from the internet, or an email attachment, or even if they are (or are not) in a "secure" location.

    It may be more relevant if the files being opened are macro enabled.

    That said, if the files are "blocked", it may prevent you from setting the Protected View mode. That, in turn, may prevent you from closing the files.

    The more I write, the less confident I feel about this as a possible cause. But, anyway, something to investigate and eliminate.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    Thanks for the response TMS...But is there something missing in my code as the same code was being effectively used earlier, if so please can you please offer that input as well..
    In regards with providing an attachment I really dont know whether that would really help in this case as the files are being extracted from a BI Tool QlikView and these files are stored in the Folder INetCache.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    I suspect it is not the code but the status of the files. Check the properties in File Manager and see if they are blocked.
    Attached Images Attached Images

  5. #5
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    There is no option such as Security in my Files..to be UNBLOCKED..

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    Well, that is that option checked and eliminated. Unfortunately, my only shot.

    The only thing I can suggest is that you put some Debug statements in the "do stuff" bit of code and see if the full path is followed through the code for all files. If not, then you need to explore why it fails to get through to the close file statement.

  7. #7
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    Is there any other code to close all open files from a Specific Folder which are open, I had also included the ENABLE CONTENT Macro to first Enable the content and then copy paste the data

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    I really cannot help. The thing to focus on is this loop:

    Please Login or Register  to view this content.
    You open a workbook, do stuff (unknown), and close the workbook. It is a very tight loop. So, if the code is not closing the workbook, the implication is that something (presumably in the do stuff code) is preventing it getting to the close statement.

    You could try taking the DisplayAlerts = False out and see what message(s) you get.

    Other than that, I am out of ideas.

  9. #9
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    Please Login or Register  to view this content.
    I am just Setting the Variable wb in the first Open statement and do nothing and then simply close this file.. as this enetire code is just to close all the Files..
    I call this Procedure in another Macro where the stuff is done. so is there something which needs to be tweaked ?

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    I honestly don't know. I don't understand why you have a loop that just opens and closes workbooks if nothing happens in between.

    I can't offer any more help but I will see if anyone else has any ideas.

  11. #11
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Close All Open Files in a Specific Folder Using VBA

    Try using
    Please Login or Register  to view this content.

  12. #12
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Close All Open Files in a Specific Folder Using VBA

    I will just go by the title.
    "Close All Open Files in a Specific Folder Using VBA "

    The assumption is you want to loop through all the open workbooks and close the ones from a specific folder.

    If you loop through all the open workbooks
    Please Login or Register  to view this content.
    To get the full name for the open workbooks
    Please Login or Register  to view this content.
    See if there is any text in the wb.full name
    Please Login or Register  to view this content.
    If specific text is in the workbook name, close the workbook.

    ThisWorkbook is the workbook with the code.
    Please Login or Register  to view this content.
    Last edited by davesexcel; 07-30-2022 at 07:52 AM.

  13. #13
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    I tried your code davesexcel with s = "*.xls*" however it does not work and how do i provide the Path for the specific Folder ?

  14. #14
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Close All Open Files in a Specific Folder Using VBA

    s = "*.xls*"

    You asked " Close All Open Files in a Specific Folder Using VBA "
    would s = "*.xls*" be in the folder name?

    Recheck my code, I show the variable: s='string in folder name

  15. #15
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    Dear Dave,

    I am unable to tweak your code to my requirement, where do I mention the Folder path and the Likely Name of all the Open Files ? All the Files which are open would start with "CH*.xls*" and in the below path MyFolder = "C:\Users\office\AppData\Local\Microsoft\Windows\INetCache"

    So please explain how do I use your code ?

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    Try this. It does not open any files, it just closes files that are from the specified folder AND have the right file name structure.

    Please Login or Register  to view this content.

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Close All Open Files in a Specific Folder Using VBA

    What is the result of
    Please Login or Register  to view this content.



  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    @snb: Error 438.

    So maybe:
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    Thank you so much TMS, this actually works the way I wanted...

    Also thanks snb, for the alternative solution..


    Regards
    e4excel

  20. #20
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    You're welcome. Thanks for the rep.

  21. #21
    Registered User
    Join Date
    11-03-2008
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    2,521

    Re: Close All Open Files in a Specific Folder Using VBA

    Dear Forum,

    This code was working perfectly well however after I upgraded my CPU at work it stopped working there's no error however the files do not close as used to earlier is there anything i need to do in terms of checking certain references in the Library which might be impacting the same as I do not recall the references which i might have checked earlier and I do not have that CPU anymore..so can someone please guide me on the same as there are around 30 Files for 1 Task and multiple tasks have multiple files open which need to be closed..

    Warm Regards
    e4excel

  22. #22
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: Close All Open Files in a Specific Folder Using VBA

    Check: MyFolder = "C:\Users\office\AppData\Local\Microsoft\Windows\INetCache"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Open all files in a folder update, save and close
    By Lukeb123 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 03-26-2022, 06:16 AM
  2. Open all files in a folder update, save and close
    By Lukeb123 in forum Excel General
    Replies: 0
    Last Post: 01-24-2022, 12:16 AM
  3. [SOLVED] Open files based on column in a specific folder, copy content and close.
    By pipoliveira in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2020, 02:52 PM
  4. [SOLVED] Open files in folder, replace comma with semicolon etc, save and close
    By soreno in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2019, 07:01 AM
  5. [SOLVED] macro to open all files in a folder, save them, then close them
    By hopefulhart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-27-2013, 06:40 PM
  6. [SOLVED] Macro to open and close all files in a folder and sub folders
    By BillDoor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-31-2012, 06:51 AM
  7. Macro to open & close mutiple xlsx files in folder
    By bernard.x in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-20-2012, 06:18 AM

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