+ Reply to Thread
Results 1 to 3 of 3

Macro to Copy .xlsm workbooks from folder and sub-solder

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

    Macro to Copy .xlsm workbooks from folder and sub-solder

    I have written code to copy .xlsm files from folder and subfolder "C:\GL Reports" to C:\Old GL Reports"


    When running my macro no .xlsm ae copied to C:\Old GL Reports"


    Please Login or Register  to view this content.

    It would be appreciated if someone could kindly amend my code
    Last edited by Howardc1001; 10-28-2019 at 03:54 AM.

  2. #2
    Registered User
    Join Date
    06-02-2018
    Location
    Italy
    MS-Off Ver
    2013-2019
    Posts
    31

    Re: Macro to Copy .xlsm workbooks from folder and sub-solder

    Your code is wrong.
    By example, your the part for move a XLSM is:
    Please Login or Register  to view this content.
    But I think that in the move operation you must use a complete path, while you pass only the path without the filename, therefore no files will moved.

    Try this:
    Please Login or Register  to view this content.
    P.S. NOT TESTED.

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

    Re: Macro to Copy .xlsm workbooks from folder and sub-solder

    thanks for the help. my apologies I only wanted to copy (Not Move) the.xlsm workbooks from the folder C:\GL downloads as well as any sub-folders to C:\old GL Downloads


    I amended my code as follows and it works perfectly

    []code] Sub Copyfiles()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim FSO As Object, fld As Object
    Dim fname As Object
    Dim sbfol As Object
    Dim fpath As String, xpath As String
    Dim tpath As String

    fpath = "C:\GL Reports"
    tpath = "C:\Old GL Reports"
    If Right(fpath, 1) <> "\" Then fpath = fpath & "\"
    If Right(tpath, 1) <> "\" Then tpath = tpath & "\"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set fld = FSO.getfolder(fpath)
    If FSO.folderExists(fld) Then
    For Each fname In fld.Files
    If Right$(fname.Name, 4) = "xlsm" Then
    xpath = fname.Path
    FSO.copyfile Source:=xpath + fname, Destination:=tpath + fname
    End If
    Next

    For Each sbfol In FSO.getfolder(fpath).subfolders
    For Each fname In sbfol.Files
    If Mid(fname.Name, InStrRev(fname.Name, ".") + 1) = "xlsm" Then

    xpath = fname.Path

    FSO.copyfile Source:=xpath, Destination:=tpath
    End If
    Next
    Next
    End If
    End Sub [/code]
    Last edited by Howardc1001; 10-28-2019 at 05:01 AM.

+ 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] Macro to list and open .xlsm files in folder and sub-folder
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-24-2019, 07:35 PM
  2. [SOLVED] Copy Range from multiple Workbooks in folder, to Summary Workbook also in same folder?
    By xlyfe in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-24-2018, 01:22 PM
  3. [SOLVED] Macro that combines serveral workbooks stops on .xlsm files
    By Happytab in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-05-2017, 09:05 AM
  4. Split .xlsm workbook into multiple xlsm workbooks w/ 1 tab each
    By JenBR in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-23-2014, 07:05 PM
  5. VBA Copy macro-enabled worksheets to all workbooks in folder
    By Kahmed2R in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-30-2013, 09:59 AM
  6. create a compressed folder or zipped folder then copy workbooks to it.
    By Ironman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-04-2012, 03:56 PM
  7. Macro - Copy 3 cells from all workbooks in a folder
    By misterzr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2010, 09:43 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