December 8, 2016
I have several files in sub folders on a network "\\account\man\
The sub folders are for eg "\\account\man\BR1\
"\\account\admin\BR2\
"\\account\admin\sammy\
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 = "\\account\man\"
'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
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
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
December 8, 2016
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 = "\\account\man\"
'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
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
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
'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 = "\\account\man\"
'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
December 8, 2016
Hi Catalin
This is beyond my VBA programming skills.
I am trying to copy all files within the network folder "\\account\man\" 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
October 5, 2010
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 \\account\man folder.
I'm assuming you have the drive mapped? If not then using the UNC format "\\account\man\*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
Please read the XCOPY doco to understand how it works
https://docs.microsoft.com/en-.....ands/xcopy
Regards
Phil
Answers Post
1 Guest(s)