+ Reply to Thread
Results 1 to 10 of 10

Macro to list and open .xlsm files in folder and sub-folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,876

    Macro to list and open .xlsm files in folder and sub-folder

    I have code to list files in C:\pull and well as sub-folder where workbook contain ACCNTS(P) in the name as well as if files are .xlsm files

    eg M_BR ACCNTS (P).xlsm



    The codes lists only .xls files in C:\pull and its sub-folder


    It would be appreciated if someone could amend my code to include only .xlsm files




     Sub List_Man_Acc_FileNames()
    
    Sheets("file names").Range("A1:C150").ClearContents
    Application.ScreenUpdating = False
    Sheets("file names").Range("A1:C1").Value = Array("File Name", "Created", "Last Modified")
    
    LoopController ("C:\pull")
    Sheets("file names").Columns.AutoFit
    
    End Sub
    
    Private Sub LoopController(sSourceFolder As String)
    'This will loop into itself, first processing the files in the folder
    'then looping into each subfolder deeper and deeper until all folders processed
    Dim Fldr As Object, FL As Object, SubFldr As Object
    
        Call ListFilesinFolder(sSourceFolder & Application.PathSeparator)
    
        Set Fldr = CreateObject("Scripting.FileSystemObject").GetFolder(sSourceFolder)
        For Each SubFldr In Fldr.SubFolders
            LoopController SubFldr.path
        Next
    
    End Sub
    
    Sub ListFilesinFolder(MyPath As String)
    Dim FSO As Object, f As Object, FLD As Object, NR As Long
    
    NR = Sheets("file names").Range("A" & Rows.Count).End(xlUp).Row
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FLD = FSO.GetFolder(MyPath).Files
    
    For Each f In FLD
        If InStr(f.Name, "ACCNTS(P)") > 0 And LCase(Right(f.Name, 5)) = ".xlsm" Then
            NR = NR + 1
            Sheets("file names").Range("A" & NR).Value = f.Name
            Sheets("file names").Range("B" & NR).Value = f.DateCreated
            On Error Resume Next
            Sheets("file names").Range("C" & NR).Value = f.DateLastModified
            On Error GoTo 0
        End If
    Next f
    
    End Sub

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to list and open .xlsm files in folder and sub-folder

    It looks like the code already looks for the .xlsm files but is qualified with a partial file name to limit those files to ones containing ACCNTS(P) in the file name. If you just want all the xlsm files then change this statement

    If InStr(f.Name, "ACCNTS(P)") > 0 And LCase(Right(f.Name, 5)) = ".xlsm" Then
    in the 'ListFilesinFolder' macro to

    If LCase(Right(f.Name, 5)) = ".xlsm" Then
    and it will list all of the .xlsm files.

    But it should not have been listing any other type files.
    Last edited by JLGWhiz; 02-22-2019 at 02:28 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,876

    Re: Macro to list and open .xlsm files in folder and sub-folder

    Thanks for the help. It now extracts all the .xlsm files


    I only need the .xlsm files containing "ACCNTS(P) in the name of the workbook for eg M_BR1 ACCNTS (P).xlsm . M_CA_ACCNTS.xlsm etc to be extracted


    Kindly amend code accordingly
    Last edited by Howardc1001; 02-22-2019 at 12:30 PM.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to list and open .xlsm files in folder and sub-folder

    Quote Originally Posted by Howardc1001 View Post
    Thanks for the help. It now extracts all the .xlsm files


    I only need the .xlsm files containing "ACCNTS(P) in the name of the workbook for eg M_BR1 ACCNTS (P).xlsm . M_CA_ACCNTS.xlsm etc to be extracted


    Kindly amend code accordingly
    Then your original code should have been producing what you wanted. But there is nothing that I can see that would produce only .xls files. Try using this line of code instead.

     If InStr(f.Name, "ACCNTS") > 0 And LCase(Right(f.Name, 5)) = ".xlsm" Then
    Last edited by JLGWhiz; 02-22-2019 at 02:30 PM.

  5. #5
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,876

    Re: Macro to list and open .xlsm files in folder and sub-folder

    Thanks for your help. Code is working perfectly

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to list and open .xlsm files in folder and sub-folder

    Quote Originally Posted by Howardc1001 View Post
    Thanks for your help. Code is working perfectly
    You're welcome,
    regards, JLG

  7. #7
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,876

    Re: Macro to list and open .xlsm files in folder and sub-folder

    Is it possible to amend code that if no .xlsm file in sub-folder within C:\pull then to extract the .xls file ?

    Some sub-folders may only have an .xls workbook



    If so kindly amend code

  8. #8
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to list and open .xlsm files in folder and sub-folder

    You can try this
    If InStr(f.Name, "ACCNTS") > 0 And LCase(Mid(f.Name, InStr(f.Name, "."))) Like ".xl*" Then
    that should then return any Excel file with 'ACCNTS' in the name.

  9. #9
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2024
    Posts
    2,876

    Re: Macro to list and open .xlsm files in folder and sub-folder

    thanks for the help. Code works perfectly

  10. #10
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to list and open .xlsm files in folder and sub-folder

    Quote Originally Posted by Howardc1001 View Post
    thanks for the help. Code works perfectly
    You're welcome,
    regards, JLG

+ 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. [SOLVED] Excel VBA code for selecting a folder then search & open specified files in that folder
    By Excelrookie_1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2021, 03:09 AM
  2. [SOLVED] VBA to save all .CSV files in a folder as .XLSM not working?
    By patdools in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-18-2019, 02:28 PM
  3. [SOLVED] Listing only xlsm files of the folder with VBA
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2017, 10:09 AM
  4. Convert csv files in folder to xlsm
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-13-2015, 09:49 PM
  5. [SOLVED] Open password protected files in folder using list of passwords
    By Shift-4 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-22-2013, 06:08 PM
  6. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  7. Macro to open up all files in certain folder
    By dpcp in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2012, 03:26 PM

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