+ Reply to Thread
Results 1 to 6 of 6

Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

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

    Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

    I have a macro to copy .xlsm files in C:\pull and its sub-folders to C:\old pull files but no files are being copied

    I adapted this from code on Ron De Bruin's Website


    It would be appreciated if someone could check & amend my code


    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

    This is the code with early binding

    PHP Code: 
    Sub Copy_Certain_Files_In_Folder()
    'This example copy all Excel files from FromPath to ToPath.
    '
    Note: If the files in ToPath already exist it will overwrite
    'existing files in this folder
        Dim FSO As Scripting.FileSystemObject
        Dim FromFolder As Scripting.Folder
        Dim fil As Scripting.File
        Dim FromPath As String
        Dim ToPath As String
        Dim FileExt As String
       
        FromPath = "C:\Pull"  '
    << Change
        ToPath 
    "C:\Old pull Files"    '<< Change

        FileExt = "xlsm"  '
    << Change
        
    'You can use *.* for all files or *.doc for Word files

    '    
    If Right(FromPath1) <> "\" Then
    '        FromPath = FromPath & "
    \"
    '    End If

        Set FSO = New Scripting.FileSystemObject
        
        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " 
    doesn't exist"
            Exit Sub
        End If
        

        Set FromFolder = FSO.getfolder(FromPath)
        If FSO.FolderExists(ToPath) = False Then
            MsgBox ToPath & " doesn'
    t exist"
            Exit Sub
        End If
        
        For Each fil In FromFolder.Files
            If FSO.getExtensionName(fil.Path) = FileExt Then
                fil.Copy ToPath & "
    \" & fil.Name
            End If
        Next fil
        MsgBox "
    You can find the files from " & FromPath & " in " & ToPath

    End Sub 
    Last edited by gringol; 06-15-2021 at 04:46 AM.

  3. #3
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

    With late binding:

    PHP Code: 
    Sub Copy_Certain_Files_In_Folder()
    'This example copy all Excel files from FromPath to ToPath.
    '
    Note: If the files in ToPath already exist it will overwrite
    'existing files in this folder
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
        Dim FileExt As String
        Dim folder As Object
        Dim fil As Object

        FromPath = "C:\Pull"  '
    << Change
        ToPath 
    "C:\Old pull Files"    '<< Change

        FileExt = "xlsm"  '
    << Change
        
    'You can use *.* for all files or *.doc for Word files

        If Right(FromPath, 1) <> "\" Then
            FromPath = FromPath & "\"
        End If

        Set FSO = CreateObject("scripting.filesystemobject")

        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn'
    t exist"
            Exit Sub
        End If
        Set Fromfolder = FSO.getfolder(FromPath)
        If FSO.FolderExists(ToPath) = False Then
            MsgBox ToPath & " 
    doesn't exist"
            Exit Sub
        End If


        For Each fil In Fromfolder.Files
            If FSO.getextensionname(fil.Path) = FileExt Then
            fil.Copy ToPath & "\" & fil.Name
            End If
        Next fil
    '    
    FSO.CopyFile FileExtDestination:=ToPath FileExt
        MsgBox 
    "You can find the files from " FromPath " in " ToPath

    End Sub 

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

    Re: Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

    thanks for the code


    When using early binding , I get run time error user defined type not defined

    Please Login or Register  to view this content.

    I need the files in the subfolders with .xlsm extension in C:\pull to be copied to C:\old pull files

    Kindly amend your code
    Last edited by Howardc1001; 06-15-2021 at 06:29 AM.

  5. #5
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

    In the early binding case, did you select the Microsoft Scripting Runtime library in the References tab?

    In the late binding case, find below the code that will look in the parent folder and the subfolders:

    Please Login or Register  to view this content.

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

    Re: Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files

    Thanks for the help-code works perfectly

    I was not sure which reference in the References tab

+ 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] 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
  3. Replies: 2
    Last Post: 11-07-2016, 11:33 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. i need a macro to pull excel files from a folder and print them
    By kmraz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2013, 06:53 PM
  6. [SOLVED] Macro to pull in files from folder that is unknown
    By SeaniGeld in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2013, 02:23 PM
  7. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 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