Excel VBA File Copy

Status
Not open for further replies.

breitnet

New Member
Joined
Jan 18, 2018
Messages
13
Hello,

I am looking for some help copying from a source file to a destination file.

sourcePath = Sheet1.Cells(121, 9).Value
Source Cell: (121, 9): "C:\Desktop\Proc_9UL_RB_10\MCD_9UL_RB_10"

DestinationPath = Sheet1.Cells(110, 13).Value
Destination Cell(110,13): "C:\Desktop\Proc_9UL_RB_10\MCD_9UL_RB_10_Audit\MCD_9UL0_RB_10"

fileExtn = Sheet1.Cells(115, 4).Value & "*.mpf"
File Extension Cell(115,4): 9UL0

Source path is only be a partial file name at the end. Ex:C:\Desktop\Proc_9UL_RB_10\MCD_9UL_RB_10\9UL0*.mpf

Actual source with full file name. Ex: C:\Desktop\Proc_9UL_RB_10\MCD_9UL_RB_10\9UL0_T_10_FSA_193RP_AA.mpf

I don't want to look the entire path because there are many variations, the only constant are the first four characters (9UL0).

I am having an issue using the If statement. IF 9UL0*.mpf exists copy to destination, If not skip file copy.

Thanks for any help!

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

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

Sheet1.Cells(115, 4).Value = Sheet1.Cells(100, 4).Value & Sheet1.Cells(110, 4).Value

fileExtn = Sheet1.Cells(115, 4).Value & "*.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

'copies source path to destination path if file exists.
MsgBox "SourcePath " & sourcePath & fileExtn & "  Destination " & DestinationPath
If FSO.fileexists(sourcePath & fileExtn) Then
FSO.copyfile Source:=sourcePath & fileExtn, Destination:=DestinationPath
End If
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,133,658
Messages
5,660,156
Members
418,555
Latest member
Ike Andoit

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