+ Reply to Thread
Results 1 to 8 of 8

Import multiple files from same directory

  1. #1
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Import multiple files from same directory

    I have a folder that contains multiple csv files, like so..

    f-100713 XXX.csv
    f-100713 XXX.csv
    f-100713 XXX.csv
    f-100713 XXX.csv
    f-100713 XXX.csv
    f-100713 XXX.csv
    f-100713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-110713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    f-120713 XXX.csv
    r-100713 XXX.csv
    r-100713 XXX.csv
    r-100713 XXX.csv
    r-100713 XXX.csv
    r-100713 XXX.csv
    r-100713 XXX.csv
    r-100713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-110713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv
    r-120713 XXX.csv


    I would like to import all files into a workbook based on 2 criteria ( r or f) and the the number that follows.
    So that (for example) all the files that contain f-100713 are saved in the same workbook.
    ...and all the files that contain r-100713 are saved in the same workbook.

    In my example of files I would end up with 6 workbooks f-100713, f-110713, f-120713, r-100713, r-110713, r-120713.
    The "XXX" part of the filename can be almost anything so I presume I would need some kind of wildcard.
    I am unsure how I can achieve this.

    Any assistance appreciated.

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Import multiple files from same directory

    Can anyone assist with how to use wildcards in selecting files?

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Import multiple files from same directory

    All characters up to or after a string has a wildcard of *. For single character wildcards, use ? for each character. I am thinking that it would be easier to get all filenames like "?-?????? ???.csv" first. I would then make a unique array for the "?-?????? " strings. Once you have that, you are ready to start imported data.

    It might be easier to help if you zipped and attached a short example master xlsm file and a csv file or two.

    To get an idea of what I mean, try running this in a new workbook on a folder with a few csv files in it.
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 07-17-2013 at 03:50 PM.

  4. #4
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Import multiple files from same directory

    KH,

    I tried your code and I kept getting "no files found".

    I use this code to import all the files into the same workbook.
    I have not been able to manipulate it so that it will split the files into separate workbooks on my criteria.

    As you can see the code creates a list of all the files in the directory. I have tried setting a variable for x (the first 2 numbers) then looping x with no success.

    Note: the file names are all f/r-100713 XXX, the XXX bit is NOT always 3 characters. The '100713' part is actually the date, I could change the file names so that the date part is a number i.e f-41465 XXX. I can do this when I create the files so it is not a big issue.

    If you [or anyone else] have any suggestions about how I could manipulate my code that would be great.
    Thanks for your assistance.


    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Import multiple files from same directory

    Attached sample of csv files
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Import multiple files from same directory

    csv sample
    Attached Files Attached Files

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Import multiple files from same directory

    Adapt this method to your QueryTables method if you like. I used the DOS command shell method of COPY to concatenate the files.

    Notice where I commented out MsgBox and Debug.Print lines. These are used during the design of the code. Feel free to delete comments like those.

    Obviously, you would run this sub, MakeCSVMasters.

    After you run this code from a workbook saved to your csv folder, look for the created Master files.

    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 07-17-2013 at 05:18 PM.

  8. #8
    Forum Contributor
    Join Date
    11-04-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    334

    Re: Import multiple files from same directory

    Thanks very much KH, your code does the job nicely and will save me many hours of manually doing the job.

+ 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. [SOLVED] Macro to Import Multiple TXT Files into workbook - User to select files/directory
    By saber007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2013, 08:43 PM
  2. Select multiple files and save each as a pdf in the same directory
    By test99 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-10-2013, 12:03 PM
  3. [SOLVED] Importing the same multiple ranges from more files in one directory
    By cedexis in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-13-2012, 05:53 PM
  4. import text files multiple from parent directory
    By Craig.Selby in forum Excel General
    Replies: 2
    Last Post: 07-11-2012, 11:41 PM
  5. Macro to format multiple files from one directory
    By sxv102 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-17-2005, 01:10 AM

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