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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,383
Office Version
  1. 365
Platform
  1. Windows
What is the partial filename?
 

breitnet

New Member
Joined
Jan 18, 2018
Messages
13
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."
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,383
Office Version
  1. 365
Platform
  1. Windows
Ok, make this change
Code:
fileExtn = "[COLOR=#ff0000]8HB0[/COLOR]*.mpf"
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,383
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback
 

Forum statistics

Threads
1,141,308
Messages
5,705,666
Members
421,403
Latest member
cmowla

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
Top