Excel VBA File Copy

breitnet

New Member
Joined
Jan 18, 2018
Messages
13
Hi, I have a macro I am working on and would like to get some help. I am trying to copy files from one folder to another folder based on a partial file name.

Here is the code I am currently using. It copies the entire contents of the folder (not what I want).

Code:
Private Sub CommandButton2_Click()
 'copy files into folder
Dim FSO As Object
Dim sourcePath As String
Dim DestinationPath As String
Dim fileExtn As String

sourcePath = Sheet1.Cells(121, 9).Value
DestinationPath = Sheet1.Cells(110, 13).Value

fileExtn = "*.mpf"

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

Set FSO = CreateObject("scripting.filesystemobject")
If FSO.folderExists(sourcePath) = False Then
    MsgBox sourcePath & "does not exist"
    Exit Sub
End If

If FSO.folderExists(DestinationPath) = False Then
    MsgBox DestinationPath & "does not exist"
    Exit Sub
End If

FSO.copyfile Source:=sourcePath & fileExtn, Destination:=DestinationPath
MsgBox "Your files have been copied"

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
What is the partial filename?
 
Upvote 0
Here are the file name examples:

8HB0_RB_10.mpf --> Folder 1
8HB1_RB_10.mpf --> Folder 2
8HB2_RB_10.mpf --> Folder 3
8HB3_RB_10.mpf --> Folder 4
8HB4_RB_10.mpf --> Folder 5

These are located in a common folder and will be copied to another folder by the first 4 characters ex: "8HB0."
 
Upvote 0
Ok, make this change
Code:
fileExtn = "[COLOR=#ff0000]8HB0[/COLOR]*.mpf"
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top