Copy multiple files to multiple folders using FileDialog in Excel vba - excel

I'm trying to build vba code to copy files from a fixed folder to another folder.
My problem is that I want to specify where to copy the files using "Application.FileDialog(msoFileDialogFolderPicker)" and i don't know to do it.
Any help would be appreciated.
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim F As Object
Set F = Application.FileDialog(msoFileDialogFolderPicker)
FromPath = "Z:\Templates\Template 2020"
ToPath = "C:\Users\ocosmele\Desktop\New folder"
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder source:=FromPath, destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub

Just adjusted the first part of your code. Please read the code's comments and adjust it to fit your needs.
Option Explicit
Sub Copy_Folder()
Dim fso As Object
Dim targetFolder As Object
Dim fromPath As String
Dim intialPath As String
Dim toPath As String
' Initialize the file system object
Set fso = CreateObject("Scripting.FileSystemObject")
fromPath = "C:\Temp\Test"
intialPath = "C:\Users\ocosmele\Desktop\New folder"
' Ask user for the destination folder
Set targetFolder = Application.fileDialog(msoFileDialogFolderPicker)
' Define the target folder dialog box properties and result
With targetFolder
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = intialPath
If .Show <> -1 Then
MsgBox "You didn't select anything"
Exit Sub
End If
toPath = .SelectedItems(1)
End With
If Right(fromPath, 1) = "\" Then
fromPath = Left(fromPath, Len(fromPath) - 1)
End If
If Right(toPath, 1) = "\" Then
toPath = Left(toPath, Len(toPath) - 1)
End If
If fso.FolderExists(fromPath) = False Then
MsgBox fromPath & " doesn't exist"
Exit Sub
End If
fso.CopyFolder Source:=fromPath, Destination:=toPath
MsgBox "You can find the files and subfolders from " & fromPath & " in " & toPath
End Sub
Let me know if it works.

Related

How do I skip a newly created folder while looping through sub folders?

I've cobbled together lots of code from various previous posts (thanks to all of you!) and I almost have a working solution.
What I would like to happen is:
user chooses a folder
a new folder is created inside that folder and some .dwg files moved to it
the code then drills down to the next folder and does the same.
My problem is that the code is drilling down into the newly created folder and creating and endless cycle. Is there a way to skip the folder that i have just created? The folder is always named "Original DWGs DD-mm-yy" so I was thinking about adding
If InStr(FromPath, "original") = 0 Then
Exit Sub
End If
But I don't think "exit sub" is the right thing to do inside the fso loop?
Option Explicit
Dim sFolder As String
Sub CommandButton1_Click()
' Open the select folder prompt
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
If sFolder <> "" Then ' if a file was chosen
Debug.Print sFolder
End If
DrillDown
End Sub
Sub DrillDown()
Dim FSO As Object 'FileSystemObject
Dim fldStart As Object 'Folder
Dim fld As Object 'Folder
Dim fl As Object 'File
Dim Mask As String
Set FSO = CreateObject("scripting.FileSystemObject") ' late binding
Set fldStart = FSO.GetFolder(sFolder) ' <-- use your FileDialog code here
Mask = "*.dwg"
For Each fld In fldStart.SubFolders
ListFolders fld, Mask
Next
End Sub
Sub ListFolders(fldStart As Object, Mask As String)
Dim fld As Object 'Folder
Dim FromPath As String
For Each fld In fldStart.SubFolders
Debug.Print fld.Path & "\"
'move all specified files from FromPath to ToPath.
'Note: It will create the folder ToPath for you
Dim FSO As Object
Dim ToPath As String
Dim FileExt As String
Dim FNames As String
Dim diaFolder As FileDialog
Dim selected As Boolean
Dim FldCheck As String
FromPath = fld.Path & "\"
ToPath = FromPath & "Original DWGs " & Format(Date, "dd-mm-yy") '<< Change only the destination folder
Debug.Print ToPath
FileExt = "*.dwg" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
MsgBox "No .dwg files in " & FromPath
'Exit Sub
GoTo Err
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
Err:
FileExt = "*.err" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .err files in " & FromPath
'Exit Sub
GoTo Bak
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'---
Bak:
FileExt = "*.bak" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .bak files in " & FromPath
'Exit Sub
GoTo Log
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'---
Log:
FileExt = "*.log" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .log files in " & FromPath
Exit Sub
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
Set diaFolder = Nothing
ListFolders fld, Mask
Next
End Sub
I've added the code as suggested. However now it loops through and creates 6 "original DWG" each inside the previous one, and moves the files into the 5th level. Then I get a path not found error?
File path after the code has run:
C:\Users\d.holpin\Desktop\Matts Data\New folder\E2000 Circuit Drawings\85100004 ELECTRICAL CIRCUIT**ARCHIVE**\Original DWGs 23-09-19\Original DWGs 23-09-19\Original DWGs 23-09-19\Original DWGs 23-09-19**Original DWGs 23-09-19**\Original DWGs 23-09-19
Files have been moved from Archive to the second to last Original DWGs (highlighted in bold)
Code as it stands is:
Option Explicit
Dim sFolder As String
Sub CommandButton1_Click()
' Open the select folder prompt
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
If sFolder <> "" Then ' if a file was chosen
Debug.Print sFolder
End If
DrillDown
End Sub
Sub DrillDown()
Dim FSO As Object 'FileSystemObject
Dim fldStart As Object 'Folder
Dim fld As Object 'Folder
Dim fl As Object 'File
Dim Mask As String
Dim test As String
Set FSO = CreateObject("scripting.FileSystemObject") ' late binding
Set fldStart = FSO.GetFolder(sFolder) ' <-- use your FileDialog code here
Mask = "*.dwg"
For Each fld In fldStart.SubFolders
test = InStr(1, fld.Name, "Original DWGs ")
Debug.Print test
If InStr(1, fld.Name, "Original DWGs ") = 0 Then ListFolders fld, Mask
Next
'For Each fld In fldStart.SubFolders
'ListFolders fld, Mask
'Next
End Sub
Sub ListFolders(fldStart As Object, Mask As String)
Dim fld As Object 'Folder
Dim FromPath As String
For Each fld In fldStart.SubFolders '2nd tme around it jump from here to the end if listfolders?
Debug.Print fld.Path & "\"
'move all specified files from FromPath to ToPath.
'Note: It will create the folder ToPath for you
Dim FSO As Object
Dim ToPath As String
Dim FileExt As String
Dim FNames As String
Dim diaFolder As FileDialog
Dim selected As Boolean
Dim FldCheck As String
FromPath = ""
FromPath = fld.Path & "\"
ToPath = FromPath & "Original DWGs " & Format(Date, "dd-mm-yy") '<< Change only the destination folder
Debug.Print ToPath
FileExt = "*.dwg" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .dwg files in " & FromPath
'Exit Sub
GoTo Err
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
Err:
FileExt = "*.err" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .err files in " & FromPath
'Exit Sub
GoTo Bak
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'---
Bak:
FileExt = "*.bak" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .bak files in " & FromPath
'Exit Sub
GoTo Log
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
'---
Log:
FileExt = "*.log" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
'MsgBox "No .log files in " & FromPath
'Exit Sub
GoTo FIN
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(ToPath) = False Then
FSO.CreateFolder (ToPath)
End If
FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
FIN:
Set diaFolder = Nothing
FromPath = ""
ToPath = ""
ListFolders fld, Mask
Next
End Sub
In DrillDown you should add the check you mentioned where you loop through the SubFolders:
For Each fld In fldStart.SubFolders
If InStr(1, fld.Name, "Original DWGs ") = 0 Then ListFolders fld, Mask
Next

Move only files with matching files names from one folder to another folder

I want to copy only the files from one folder “the FromPath” that have the same file name (with different extensions) as in another folder the “the ToPath”. Only the shared file named files will be moved. I think the code would have to first look in the ToPath folder to get the names of the files and then cross reference those in the “FromPath” folder.
Thanks
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Val As String
Dim i As Integer
FromPath = "C:\Users\rossi\Desktop\Production files\" & (Me.ListBox1) '<< Change
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) = True Then
Val = ListBox2.List(i)
End If
Next i
FileExt = "*.sli*" '<< Change
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
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2.List(i)) '<< Change
If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End If
Next i
End Sub
You pretty much have it. I made a couple of small additions. First I make a unique list of local files in the colFiles collection. I did this because you are copying to a remote server. I think it will probably be quicker this way. Once you have the list of local files, you simply loop through the collection checking to see if they exist in the remote folder, and copy them if they do.
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Val As String
Dim i As Integer
Dim x As Integer
Dim colFiles As New Collection
Dim strFilename As String
FromPath = "C:\Users\rossi\Desktop\Production files\" & (Me.ListBox1) '<< Change
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) = True Then
Val = ListBox2.List(i)
End If
Next i
FileExt = "*.sli*" '<< Change
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
'Create a list of local filenames
strFilename = Dir(FromPath & "*" & FileExt) 'Corrected
While strFilename <> ""
colFiles.Add Left(strFilename, _
InStr(1, strFilename, ".", vbBinaryCompare) - 1), _
Left(strFilename, InStr(1, strFilename, ".", vbBinaryCompare) - 1)
strFilename = Dir()
Wend
Set FSO = CreateObject("scripting.filesystemobject")
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2.List(i)) '<< Change
If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
'Now loop through our list of files to see if they exist on the remote server
For x = 1 To colFiles.Count 'Corrected
If FSO.FileExists(ToPath & colFiles.item(x) & FileExt) Then
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
End If
Next
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End If
Next i
End Sub

How to use multiple selections from VBA Userform Multiselect Listbox in path

I have created a VBA userform, by manipulating code i found from a website, for coworkers to transfer files from selected folder from one listbox to another folder in a second listbox. The folders that populate in the listboxes change daily. It works fine for both listboxes with fmSingleSelect but i cannot figure out how to run it properly with a fmMultiSelect property on the second listbox (Yes, i changed the property to fmMultiSelect on the second listbox).
It would save time to be able to multiselect the projects folder and run the transfers simultaneously.
Below is the code for single select and commented out some code i was working with for multiselect
Also an image is below code
Thanks
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Value As String
Dim i As Integer
FromPath = "C:\Users\us-lcn-dataprep03\Desktop\Production files\" & (Me.ListBox1) '<< Change
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2) '<< Change
' For i = 0 To ListBox2.Items.Count - 1
' If ListBox2.Items(i).Selected = True Then
' Val = ListBox2.Items(i).Value
' End If
'Next i
FileExt = "*.sli*" '<< Change
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
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub
Userform list boxes
The following code is a "minimal change" alteration to your code so that it should handle copying the files from one directory to multiple directories:
Private Sub CmdBtn_transfer_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim Value As String
Dim i As Integer
FromPath = "C:\Users\us-lcn-dataprep03\Desktop\Production files\" & (Me.ListBox1) '<< Change
FileExt = "*.sli*" '<< Change
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
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
ToPath = "\\bego.hb\MED_PRODUCTION\USA_Datapreparation\" & (Me.ListBox2.List(i)) '<< Change
If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End If
Next i
End Sub
All I did was move your commented out code re looping through the selected items in ListBox2 so that it was wrapped around the parts of the code which are affected by the ToPath. (Note: The MsgBox is within the loop - you may wish to move it outside the loop but, if you do, you may want to make the message more generic - e.g. "Your files have been moved as requested".)
I also corrected some mistakes in your commented code:
ListBox2.Items.Count should be ListBox2.ListCount
ListBox2.Items(i).Selected should be ListBox2.Selected(i)
ListBox2.Items(i).Value should be ListBox2.List(i)

Excel macro to move all subfolders to another location

I only know to write a macro to copy a file from Folder A to Folder B
FileCopy "C:\Documents\Folder A\test.xlsx", "C:\Documents\Folder B\test.xlsx"
Could someone help for an Excel macro to MOVE all sub-folders and files under "Folder A" to "Folder B". Thank you
Pl Try This: Change folders as per your requirements.
Sub Copy_Folder()
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Process_Contract_Notes" '<< Change
ToPath = "C:\Process" '<< Change
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub

excel Macro to copy one folder to another folder with folder name entered by user

I am trying to copy a complete folder into a new folder through excel macro, but i need the new folder name to be entered by the user every time
This is the current code i have that copies to a permanent/static folder
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\3- FINAL Country Files\1" '<< Change
ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Weekly Back" '<< Change
Application.CutCopyMode = False
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
I worked out a way for the user to enter a folder name, but unable to link this name to the new folder being created
Dim strName As String
Dim WeekStr1 As String
Dim WeekStr2 As String
Reenter:
strName = InputBox(Prompt:="Enter the week you would like to update", _
Title:="Week Selection.", Default:="0")
If strName = vbNullString Then
Exit Sub
Else
Select Case strName
Case Else
MsgBox "Incorrect Entry."
GoTo Reenter
End Select
End If
I need the "StrName" to be placed in the following context for it to work, but cant seem to get the right syntax
ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Week "StrName"" '<< Change
Perhaps like below?
ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Week" & StrName
To concatenate Text/String simply use & (ampersand) . +(plus) works too but I'm comfortable with &
Thank you, I figured out where the issue was :)
Basically i had to add StrName to
FSO.CopyFolder Source:=FromPath, Destination:=ToPath & strName
Sometimes the simplest issues are the worst lol. thanks for your help
Below is the final code for future reference in case anyone else gets stuck
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim strName As String
Dim WeekStr1 As String
Dim WeekStr2 As String
FromPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\3- FINAL Country Files\KSA" '<< Change
ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Week"
Application.CutCopyMode = False
Reenter:
strName = InputBox(Prompt:="Enter the week you would like to update", _
Title:="Week Selection.", Default:="0")
If strName = vbNullString Then
MsgBox "Incorrect Entry."
GoTo Reenter
End If
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath & strName, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath & strName
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath & strName
'''''******you need to select folder to copy to different location, first select file
folder then select newfolderpath
***********''''''''''' you can copy all files through subfolder into one folder
Sub Copyfilesintosub()
Dim fso As Scripting.FileSystemObject
Dim fillfolder As Scripting.Folder
Dim fill As Scripting.File
Dim filefolder As Folder
Dim filepath As String
Dim abc As String
Dim subfolder As Folder
Dim mesboxresule As VbMsgBoxResult
Dim fd As FileDialog
Dim ivalu As String
Dim dum As String
Dim inp As String
Dim fpath As String
Dim chfail As Boolean
Set fso = New Scripting.FileSystemObject
mesboxresule = MsgBox("select yes to pick folder, else no", vbYesNo + vbInformation, "Decicion making by " & Environ("Username"))
If mesboxresule = vbYes Then
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.ButtonName = "Go"
fd.Title = "Please Select Folder to copy data"
fd.InitialFileName = Environ("Userprofile") & "\" & "\Desktop"
fd.InitialView = msoFileDialogViewProperties
If chfail = fd.Show Then
MsgBox "you didn't pick folder, Please try again", vbCritical + vbApplicationModal + vbSystemModal, "Please run again"
Exit Sub
Else
filepath = fd.SelectedItems(1)
End If
ElseIf mesboxresule = vbNo Then
filepath = Environ("UserProfile") & "\Desktop\" & Environ("Username")
End If
Set fillfolder = fso.GetFolder(filepath)
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.ButtonName = "Go"
fd.Title = "Please Select Folder to paste data"
fd.InitialFileName = Environ("Userprofile") & "\" & "\Desktop"
fd.InitialView = msoFileDialogViewProperties
If chfail = fd.Show Then
MsgBox "you didn't pick folder, Please try again", vbCritical + vbApplicationModal + vbSystemModal, "Please try again"
Exit Sub
Else
fpath = fd.SelectedItems(1)
End If
For Each subfolder In fillfolder.SubFolders
Debug.Print subfolder.Name
For Each fill In subfolder.Files
dum = fill.Name
ivalu = InStr(1, dum, "%")
If ivalu > 0 Then
ActiveCell.Value = fill.Name
ivalu = ActiveCell.Replace("%", "")
dum = ActiveCell.Value
fill.Name = dum
End If
If fill Like "*.xlsx" Or fill Like "*.xls" Or fill Like "*.xlsm" Then
If Not fso.FileExists(fpath & "\" & fill.Name) Then
fill.Copy fpath & "\" & fill.Name
End If
End If
Next fill
Next subfolder
Dim count As Long
MsgBox "done"
Dim hg As Scripting.File
Dim hgg As Scripting.Folder
Dim count1 As Long
Set hgg = fso.GetFolder(fpath)
Dim subfolder1 As Folder
For Each subfolder1 In hgg.SubFolders
Next subfolder1
For Each fill In fillfolder.Files
Debug.Print fill.Name
dum = fill.Name
ivalu = InStr(1, dum, "%")
If ivalu > 0 Then
ActiveCell.Value = fill.Name
ivalu = ActiveCell.Replace("%", "")
dum = ActiveCell.Value
fill.Name = dum
End If
If fill Like "*.xlsx" Or fill Like "*.xls" Or fill Like "*.xlsm" Then
If Not fso.FileExists(fpath & "\" & fill.Name) Then
fill.Copy fpath & "\" & fill.Name
End If
End If
Next fill
Dim count2 As Long
count2 = count2 + hgg.Files.count
Dim finalcount As Long
finalcount = count2
MsgBox finalcount
MsgBox "Done", vbExclamation, "copying data Succesful"
End Sub

Resources