Results 1 to 6 of 6

VBA to copy files that contain certain text within filename from one folder to another

Threaded View

  1. #1
    Registered User
    Join Date
    05-17-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    15

    VBA to copy files that contain certain text within filename from one folder to another

    Hi All,

    I need a VBA that can go through a certain folder and copy files that contain a certain text into another folder. For example.

    I have 100 PDF files in 'FOLDER A'

    I need to go through 'FOLDER A' and find the PDF files that contain the word 'Sydney' and copy it in folder called 'FOLDER - Sydney'. Then find the files that contain the word 'Melbourne and copy it into 'FOLDER - Melbourne', and so on untill i have completed the string requirements.

    If you can help me with this it would be great!!!!


    Thanks Guys!



    Below is a VBA i tried to manipulate, however it did not work and dont know how to loop it into another target folder.

    sSrcFolder = ActiveSheet.Cells(2, 1) - Folder with all files
    sTgtFolder = ActiveSheet.Cells(2, 2) - Folder to copy into
    Set rPatterns = ActiveSheet.Range("E2:E15")
    - list of words i was looking for.

    Sub CopyFiles_Containing()
        Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
        Dim c As Range, rPatterns As Range
        Dim bBad As Boolean
        
        sSrcFolder = ActiveSheet.Cells(2, 1)
        sTgtFolder = ActiveSheet.Cells(2, 2)
        
        Set rPatterns = ActiveSheet.Range("E2:E15").SpecialCells(xlConstants)
        For Each c In rPatterns
            sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
            If sFilename = "" Then
                c.Interior.ColorIndex = 3
                bBad = True
            Else
                While sFilename <> ""
                    FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
                    sFilename = Dir()
                Wend
            End If
        Next c
        If bBad Then MsgBox "Some files were not found. " & _
            "These were highlighted for your reference."
    End Sub
    Last edited by Leith Ross; 07-24-2013 at 10:55 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  2. excel macro to copy data from multi text files in a folder to a single work sheet
    By dil_se in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2012, 08:10 AM
  3. open all files in a folder with fixed number of characters in filename
    By Telemick in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-18-2012, 03:31 AM
  4. [SOLVED] Copy Files to other folder
    By TISR in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-01-2006, 03:50 AM
  5. [SOLVED] How to copy 30 csv files from a folder to another folder
    By ddiicc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2005, 05: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