+ Reply to Thread
Results 1 to 3 of 3

VBA code to locate specific files in a path, make a copy of that file in a new folder HELP

  1. #1
    Registered User
    Join Date
    03-24-2020
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    4

    Unhappy VBA code to locate specific files in a path, make a copy of that file in a new folder HELP

    Hello all

    I am new here and would like help with macro that i cannot seem to find why it is not working with me anymore.

    So this Macro is used to locate specific files that are in orders sheet, files are normally images or PDF files saved within a specific folders or path, the problem is that i only want those on Orders sheet to be searched for in a path and put into a new folder, but this is not working now for me, and i dont know how to fix it. the results that are found normally gets highlighted in the orders sheet but this has all stopped now. and it is frustrating me that when it launches it stops but the folder gets created without any results in there.

    Public Sub HCSyncreonPOD()

    Dim DT As String
    Dim Source As String
    Dim Dest As String
    Dim vFiles As Variant
    Dim vFile As Variant
    Dim rCell As Range
    Dim oFSO As Object
    Dim FileFound As Boolean
    Dim FF As Long

    On Error Resume Next
    FF = FreeFile
    DT = Format(Now, "dd.mm.yyyy")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Source = "\\CZCHONYSVM007.prg-dc.dhl.com\BS50769_Galactica\Syncreon_Hard_Copies\SYNCREON HARD COPIES"
    Dest = "\\CZCHONYSVM007.prg-dc.dhl.com\BS50769_Galactica\Syncreon_Hard_Copies\HC-POD Verbal Found on " & DT


    If Dir(Dest, vbDirectory) = "" Then MkDir Dest

    'Get the full path name of all PDF files in the source folder and subfolders.
    vFiles = EnumerateFiles(Source, "*")

    With Worksheets("Orders")
    'Look at each cell containing file names.
    For Each rCell In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
    FileFound = False 'Assume the file hasn't been found.
    'Check each value in the array of files.
    For Each vFile In vFiles
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Use this line if the file name in the sheet exactly match the file name in the array. '
    '8152 and 100_8152.pdf are not a match, 8152 and 8152.pdf are a match. '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'If rCell & ".pdf" = FileNameOnly(vFile) Then

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Use this line if the file name in the sheet should appear in the file name in the array. '
    '8152 and 100_8152.pdf are a match, 1852 and 8152.pdf are a match. '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If InStr(FileNameOnly(vFile), rCell.Value) > 0 Then

    'If found copy the file over and indicate it was found.

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This line will use the rcell value to name the file. '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'oFSO.CopyFile vFile, Dest & "" & rCell & "*.pdf"

    ''''''''''''''''''''''''''''''''''''''
    'This line will not rename the file. '
    ''''''''''''''''''''''''''''''''''''''
    On Error Resume Next
    oFSO.CopyFile vFile, Dest & "" & FileNameOnly(vFile)
    rCell.Interior.Color = RGB(250, 255, 25)
    FileFound = True
    End If
    Next vFile

    'Any file names that aren't found are appended to the text file.
    'If Not FileFound Then
    ' Open (Dest & "" & "MissingFiles.txt") For Append As #FF ' creates the file if it doesn't exist
    ' Print #FF, rCell ' write information at the end of the text file
    ' Close #FF
    'End If
    Next rCell
    End With
    Sheets("Control").Select
    MsgBox "Transfer complete, You got all Syncreon HC POD`s :)"
    End Sub

    Public Function EnumerateFiles(sDirectory As String, _
    Optional sFileSpec As String = "*", _
    Optional InclSubFolders As Boolean = True) As Variant

    EnumerateFiles = Filter(Split(CreateObject("WScript.Shell").Exec _
    ("CMD /C DIR """ & sDirectory & "*." & sFileSpec & """ " & _
    IIf(InclSubFolders, "/S ", "") & "/B /A:-D").StdOut.ReadAll, vbCrLf), ".")

    End Function

    Public Function FileNameOnly(ByVal FileNameAndPath As String) As String
    FileNameOnly = Mid(FileNameAndPath, InStrRev(FileNameAndPath, "") + 1, Len(FileNameAndPath))
    End Function

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VBA code to locate specific files in a path, make a copy of that file in a new folder

    Can you attach a short Excel sample to permit the macro you displayed to play.
    It seems you forgot the Code's tags .
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    03-24-2020
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: VBA code to locate specific files in a path, make a copy of that file in a new folder

    Hi PCI and everyone, i hope that you have had a great Christmas and holidays.

    Right it would not allow me to attach the macro as i need to post a few posts before i can actually do that. please do point out the issue, and how to fix it if you are able to.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. vba code to copy specific pdf files to a specific folder which is scattered on drive
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2022, 03:54 PM
  2. move files from Source Folder to Destination Folder, based on file name & Path
    By seenai in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2020, 09:29 AM
  3. VBA Code to copy Files From Onedrive Folder To Local Path
    By hrayani in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-08-2020, 01:29 PM
  4. Replies: 2
    Last Post: 12-29-2015, 04:19 AM
  5. [SOLVED] Copy files from one folder to another folder (path in textbox)
    By yoko21 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2014, 02:45 AM
  6. Code to list the folder path and sub folder path of a specific file
    By kalai1587 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2013, 03:51 AM
  7. consolidating excel files into one file not using hard code of folder path
    By rrakkki in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-21-2011, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1