I have a folder with numerous linked workbooks. I would like to store a master copy of it within the C:\ drive. When someone needs to use it they would click on the below macro to copy the folder, ask what the new name will be and place it on the desktop for use. The below code cycles through but does not place the folder on the desktop. It just seems to disappear and does not copy the original
Hoping someone can help??
Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim strName As String
FromPath = "C:\v4 Master Operations Folder"
ToPath = "C:\Users\Owner\Desktop"
Application.CutCopyMode = False
Reenter:
strName = InputBox(Prompt:="Enter the name of your operation", _
Title:="Operation.", Default:=" ")
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
End Sub
It looks like the problem is on this line:
FSO.CopyFolder Source:=FromPath, Destination:=ToPath & strName
You are setting your Destination variable equal to ToPath & strName, so if the user enters "My name" then it would be "C:\Users\Owner\DesktopMy Name". You need to put a slash in there: Destination:=ToPath & "\" & strName
Related
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
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)
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
In the FromPath there are 4 xlsx files that I would like to move to the variable MyDirectory. Currently the vba runs but when it gets to this point the files remain in the temp directory (FromPath) and do not get moved and I am not sure why. Thank you :).
vba
'TRANSFER FROM TEMP '
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Users\cmccabe\Desktop\EmArray\*.xlsx"
ToPath = "MyDirectory"
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
edit
Private Sub CommandButton21_Click()
Dim MyBarCode As String ' Enter Barcode
Dim MyScan As String ' Enter ScanDate
Dim MyDirectory As String
'GET USER INPUT '
Line1:
MyBarCode = Application.InputBox("Please enter the last 5 digits of the barcode", "Bar Code", Type:=2)
If MyBarCode = "False" Then Exit Sub 'user canceled
Do
MyScan = Application.InputBox("Please enter scan date", "Scan Date", Date - 1, Type:=2)
If MyScan = "False" Then Exit Sub 'user canceled
If IsDate(MyScan) Then Exit Do
MsgBox "Please enter a valid date format. ", vbExclamation, "Invalid Date Entry"
Loop
'CREATE NEXUS DIRECTORY AND VERIFY FOLDER '
MyDirectory = "N:\1_DATA\MicroArray\NexusData\" & "2571683" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy") & "\"
If Dir(MyDirectory, vbDirectory) = "" Then
MkDir MyDirectory
Else
MsgBox ("Already exsists! Please enter again")
GoTo Line1
End If
' TRANSFER FILES '
Dim MyFile As String
MyFile = Dir("C:\Users\cmccabe\Desktop\EmArray\*.xlsx")
Do Until MyFile = ""
Name "C:\Users\cmccabe\Desktop\EmArray\*.xlsx" & MyFile As "N:\1_DATA\MicroArray\NexusData\" & "2571683" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy") & MyFile
MyFile = Dir
Loop
End Sub
Have code that finds all xlsx files in a folder for other purposes, runs all the files through a for/next loop and finds those with .xlsx at the end, this will be really slow and inefficent if you've got millions of files to sort through.
Dim f As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.getfolder(Folder).Files
If Left(f.Name, Len(ContractNumber)) = ContractNumber And Right(f.Name, 4) = "xlsx" Then
f.CopyFile Source:=FromPath, Destination:=ToPath
End If
Next
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