Forum

Macro to copy speci...
 
Notifications
Clear all

Macro to copy specific workbooks from sub-folders

8 Posts
3 Users
0 Reactions
250 Views
(@howardc)
Posts: 54
Trusted Member
Topic starter
 

I have several files in sub folders on a network "\accountman

The sub folders are for eg "\accountmanBR1

"\accountadminBR2

"\accountadminsammy

Etc

I would like all files that contain "Template 2020.xlsm to be copied from the sub-folders for eg BR Sales Report template 2020.xlsm to C:Sales Reports

It would be appreciated if someone could amend my code

Sub CopyWorkbookstoanotherFolder()

'Declare Variables
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String

'This is Your File Name which you want to Copy
sFile = "*Template.2020.xlsm*"

'Change to match the source folder path
sSFolder = "\accountman"

'Change to match the destination folder path
sDFolder = "C:Sales Reports"

'Create Object
Set FSO = CreateObject("Scripting.FileSystemObject")

'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found", vbInformation, "Not Found"
   
'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
    FSO.CopyFile (sSFolder & sFile), sDFolder, True
    MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
Else
    MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If

End Sub

 
Posted : 20/08/2020 1:23 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi Howard,

You can also loop through the main folder subfolders:

Dim SbFld as object, FileObject as Object

For each SbFld in fso.GetFolder(sSFolder).SubFolders 'loop through subfolders

    For each FileObject in fso.GetFolder(SbFld).Files 'loop through files in each subfolder

           If FileObject.Name like "*Template*2020*xlsm" then

           'copy it, we found a file matching the template

           End IF    

   Next FileObject

Next SbFld

 
Posted : 22/08/2020 2:23 am
(@howardc)
Posts: 54
Trusted Member
Topic starter
 

Sorry for only getting back to you now, but but been incredibly busy

 

I incoroprated your code with mine and get a compile error "Next without for"

 

Kindly amend code below

 

Sub sbCopyingAFile()

'Declare Variables
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim SbFld As Object
Dim FileObject As Object
For Each SbFld In FSO.GetFolder(sSFolder).SubFolders 'loop through subfolders

For Each FileObject In FSO.GetFolder(SbFld).Files 'loop through files in each subfolder
If FileObject.Name Like "*Template*2020*xlsm" Then

'This is Your File Name which you want to Copy
'sFile = "*Template.2020.xlsm*"

'Change to match the source folder path

sSFolder = "\accountman"

'Change to match the destination folder path
sDFolder = "C:Sales Reports"

'Create Object
Set FSO = CreateObject("Scripting.FileSystemObject")

'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
MsgBox "Specified File Not Found", vbInformation, "Not Found"

'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), sDFolder, True
MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If
Next FileObject

Next SbFld
End Sub 

 
Posted : 25/09/2020 11:38 pm
(@catalinb)
Posts: 1937
Member Admin
 

There are many more issues in your code.

You have 2 lines with If...Then, but only 1 End If closing line.

Second issue:
move static parameters like sfile, sSFolder, sDFolder outside the loop, before loops. Makes no sense to set these parameters within loops, they will be set at every file found in those subfolders.

Another issue:

In order to use the file system object with FSO.GetFolder(sSFolder).SubFolders, it's a good idea to... create the object before using it, will not work otherwise. Before the loops, you have to create the FSO object.

 

And another one:

'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then

This check is a nonsense. Because we use For Each FileObject In FSO.GetFolder(SbFld).Files, this means that we deal only with EXISTING files, checking an EXISTING file if it exists is simply redundant.

I can only guess that what you was trying to do is to see if the file naming structure corresponds to your convention sFile.
The code already does that in this line: If FileObject.Name Like "*Template*2020*xlsm" Then

Sub sbCopyingAFile()
'Declare Variables
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim SbFld As Object
Dim FileObject As Object

'This is Your File Name which you want to Copy
sFile = "*Template*2020.xlsm*"
'Change to match the source folder path
sSFolder = "\accountman"
'Change to match the destination folder path
sDFolder = "C:Sales Reports"
'Create Object
Set FSO = CreateObject("Scripting.FileSystemObject")

For Each SbFld In FSO.GetFolder(sSFolder).SubFolders 'loop through subfolders
    For Each FileObject In FSO.GetFolder(SbFld).Files 'loop through files in each subfolder
        If FileObject.Name Like sFile Then
            'Copying If the Same File is Not Located in the Destination Folder
            If Not FSO.FileExists(sDFolder & FileObject.Name) Then
                FSO.CopyFile (FileObject.Path), sDFolder & FileObject.Name, True
                MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
            Else
                MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
            End If
        End If
    Next FileObject
Next SbFld
End Sub

 
Posted : 26/09/2020 3:20 am
(@howardc)
Posts: 54
Trusted Member
Topic starter
 

Hi Catalin

 

This is beyond my VBA programming skills.

 

I am trying to copy all files within the network folder "\accountman" as well as its sub-folders that contain ""*Template*2020.xlsm*" for e.g. BR1 Sothern template Sales 2020.xlsm, BR2 Sothern template Sales 2020.xlsm etc and copy these to  "C:Sales Reports"

 

It would be appreciated if you would kindly provide me with the code to do this

 
Posted : 26/09/2020 6:39 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Howard,

It's much easier to do this kind of thing directly in the command line.  Writing VBA to call FSO is just using the Windows Shell/Command Line anyway and way more work than it needs to be.

Start a Command Prompt and all you need is a line like this

xcopy "x:man*template*2020.xlsm" "c:Sales Reports" /S /I /C

Where x:man is a drive mapping to your \accountman folder. 

I'm assuming you have the drive mapped? If not then using the UNC format "\accountman*template*2020.xlsm" should also work.

If you need to copy files periodically you can either place this line of code into a batch file or use VBA to call a Shell command

VBA Shell Function

Please read the XCOPY doco to understand how it works

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

Regards

Phil

 
Posted : 06/10/2020 7:51 pm
(@catalinb)
Posts: 1937
Member Admin
 

It would be appreciated if you would kindly provide me with the code to do this

Hi Howard,

That's exactly what I did, you have in the previous message the corrected code, all you had to do is to paste it into your file and run it.

 
Posted : 07/10/2020 12:45 am
(@howardc)
Posts: 54
Trusted Member
Topic starter
 

Many Thanks for your help

 
Posted : 07/10/2020 7:35 am
Share: