VBA Code Copy Files from One folder to Another

mielkew

New Member
Joined
Sep 14, 2015
Messages
21
2.5.0.0
Hello guys,

I have a VBA code which is working like CUT and PASTE, I was hoping to change it form CUT and PASTE to COPY and PASTE..

any help is very much appreciated :):)

Code:
Option Explicit


Sub MoveFiles()
   Dim i As Long
   Dim numRows As Long
   Dim oldFileName As String
   Dim newFileName As String
   
   With Sheets("Sheet1")
      'get number of rows to loop through
      numRows = .Range("D" & .Rows.Count).End(xlUp).Row
      
      For i = 4 To numRows
         '====================
         'old file
         '====================
         'build the path, test for end backslash in path for OLD filename
         oldFileName = .Range("D" & i).Value
         If Right(oldFileName, 1) <> "\" Then oldFileName = oldFileName & "\"
         
         'add the file name
         oldFileName = oldFileName & .Range("C" & i).Value
         
         'does the file exist?
         If Not FileFolderExists(oldFileName) Then
            'highlight cell when OLD file doesn't exist, and skip to next row
            .Range("D" & i).Font.Color = vbRed
            GoTo NextLoopIndex
         End If


         '==============
         'new file
         '============
         'test for end backslash in path for NEW filename
         newFileName = .Range("E" & i).Value
         If Right(newFileName, 1) <> "\" Then newFileName = oldFileName & "\"
         CreateFullPath newFileName
         'add on the filename
         newFileName = newFileName & .Range("C" & i).Value
         
         '==================
         'move file
         '===============
         Name oldFileName As newFileName
         
NextLoopIndex:
         oldFileName = ""
         newFileName = ""
      Next i
   End With
End Sub




Private Sub CreateFullPath(ByVal FilePath As String)
   Dim Folders  As Variant
   Dim tmp As String
   Dim i As Long
   
   'does the folder exist?
   If Not FileFolderExists(FilePath) Then
      If MsgBox("File path does not exist. Do you want to create it?", _
                Buttons:=vbYesNo + vbQuestion) = vbYes Then


         'build the folders
         Folders = Split(FilePath, "\")
         For i = LBound(Folders) To UBound(Folders) - 1
            tmp = tmp & Folders(i) & "\"
            If Not FileFolderExists(tmp) Then MkDir tmp
         Next i
      Else
         Exit Sub
      End If
   End If
End Sub




Private Function FileFolderExists(ByVal FilePath As String) As Boolean
   If Not Dir(FilePath, vbDirectory) = vbNullString Then FileFolderExists = True
End Function
2.5.0.0
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi,

try changing this line

Code:
Name oldFileName As newFileName


to this

Code:
FileCopy oldFileName, newFileName

and see if this does what you want

Dave
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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