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 is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Status
Not open for further replies.

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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