VBA - I have a column with files paths to a pdf document , i want to copy the file from the filepath and paste into a new folder

richie2989

New Member
Joined
Nov 13, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am fairly new to using vba, can someone help me - I have a column with all filled with filepaths to a specific pdf document, I want to copy the file using the filepath and paste into a new folder.

Hope this makes sense
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
VBA Code:
Option Explicit

Sub CopyFiles()
    'https://www.mrexcel.com/board/threads/vba-i-have-a-column-with-files-paths-to-a-pdf-document-i-want-to-copy-the-file-from-the-filepath-and-paste-into-a-new-folder.1151035/
    
    Dim sDestinationPath As String
    Dim rngListStart As Range
    Dim sFilePathNameExt As String
    Dim lLastRow As Long
    Dim lIndex As Long
    Dim sFileNameExt As String
    
    'This code assumes the list of FilePathNameExt to copy starts in cell A2
    '  If that is not the case change A2 in the next row to the correct range
    Set rngListStart = ActiveSheet.Range("A2")
    
    'The code also assumes that the list continues in that column and that
    '  there is nothing in that column below the last entry in the list
    
    'Replace the next row with the destination for the files
    sDestinationPath = Environ("userprofile") & "\Documents\"
    
    If Right(sDestinationPath, 1) <> "\" Then sDestinationPath = sDestinationPath & "\"
    
    lLastRow = Cells(Rows.Count, rngListStart.Column).End(xlUp).Row
    
    For lIndex = rngListStart.Row To lLastRow
        sFilePathNameExt = Cells(lIndex, rngListStart.Column).Value
        sFileNameExt = Mid(sFilePathNameExt, InStrRev(sFilePathNameExt, "\") + 1)
        If Dir(sFilePathNameExt, vbNormal) <> vbNullString Then
            FileCopy sFilePathNameExt, sDestinationPath & sFileNameExt
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,840
Messages
6,121,895
Members
449,058
Latest member
Guy Boot

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