+ Reply to Thread
Results 1 to 8 of 8

Import multiple files from same directory

Hybrid View

  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.
    'http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names_using_vba/
    Sub Test_GetFileList()
        Dim p As String, x As Variant, i As Integer
    
        p = ThisWorkbook.Path & "\?-?????? *.csv"
        x = GetFileList(p)
        Select Case IsArray(x)
            Case True 'files found
                MsgBox UBound(x), , "Count of Found Files"
                Sheets("Sheet1").Range("A:A").Clear
                For i = LBound(x) To UBound(x)
                    Sheets("Sheet1").Cells(i, 1).Value = x(i)
                Next i
            Case False 'no files found
                MsgBox "No matching files"
        End Select
    End Sub
    
    Function GetFileList(FileSpec As String) As Variant
    '   Returns an array of filenames that match FileSpec
    '   If no matching files are found, it returns False
    
        Dim FileArray() As Variant
        Dim FileCount As Integer
        Dim FileName As String
        
        On Error GoTo NoFilesFound
    
        FileCount = 0
        FileName = Dir(FileSpec)
        If FileName = "" Then GoTo NoFilesFound
        
    '   Loop until no more matching files are found
        Do While FileName <> ""
            FileCount = FileCount + 1
            ReDim Preserve FileArray(1 To FileCount)
            FileArray(FileCount) = FileName
            FileName = Dir()
        Loop
        GetFileList = FileArray
        Exit Function
    
    '   Error handler
    NoFilesFound:
        GetFileList = False
    End Function
    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.


    Sub Testcsv()
    Dim z  As Long, e As Long
    Dim f As String, m As String, n As String
    Sheets("Sheet1").Select
    Cells(1, 1) = "=cell(""filename"")"
    Cells(1, 2) = "=left(A1,find(""["",A1)-1)"
    Cells(2, 1).Select
    f = Dir(Cells(1, 2) & "*.csv")
    Do While Len(f) > 0
    ActiveCell.Formula = f
    ActiveCell.Offset(1, 0).Select
    f = Dir()
    Loop
    z = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    For e = 2 To z
    n = Sheets("Sheet1").Cells(e, 1)
    If n <> ActiveWorkbook.Name Then
    If Len(n) < 35 Then
    m = Left(n, Len(n) - 4)
    Else
    m = Left(n, 31)
    End If
    Sheets.Add.Name = m
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;file:///" & Sheets("Sheet1").Cells(1, 2) & Sheets("Sheet1").Cells(e, 1), _
            Destination:=Sheets(m).Range("A1"))
            .Name = "bt"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
                        End With
    End If
    Next e
    
    End Sub

  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.

    Option Explicit
    
    ' http://www.excelforum.com/excel-programming-vba-macros/939533-import-multiple-files-from-same-directory.html?p=3326646
    
    'http://spreadsheetpage.com/index.php/tip/getting_a_list_of_file_names_using_vba/
    Sub MakeCSVMasters()
        Dim p As String, x() As Variant, i As Integer
    
        p = ThisWorkbook.Path & "\?-?????? *.csv"
        x() = GetFileList(p)
        Select Case IsArray(x)
            Case True 'files found
                'MsgBox UBound(x), , "Count of Found Files"
                'Sheets("Sheet1").Range("A:A").Clear
                'For i = LBound(x) To UBound(x)
                '    Sheets("Sheet1").Cells(i, 1).Value = x(i)
                'Next i
                MakingCSVMasters ThisWorkbook.Path, x()
            Case False 'no files found
                MsgBox "No matching files"
        End Select
    End Sub
    
    Sub MakingCSVMasters(pPath As String, a() As Variant)
      Dim x() As Variant, y() As Variant, xv As Variant, yv As Variant
      Dim z() As Variant, zv As Variant, i As Integer, s As String
      
      x() = UniqueArray(a)
      'MsgBox Join(x, vbLf)
      
      ReDim y(LBound(x) To UBound(x))
      i = LBound(x) - 1
      For Each xv In x()
        'MsgBox Join(Filter(a(), xv, True), " ")
        'Add Parent Path, pPath, and embed quotes around full file name
        'and bulid string for COPY
        s = vbNullString
        i = i + 1
        For Each zv In Filter(a(), xv, True)
          '+ sign is the COPY concatenation operator
          s = s & "+" & """" & pPath & "\" & zv & """"
        Next zv
        y(i) = Right(s, Len(s) - 1) 'Trim first "+" character
        'MsgBox Join(y(), vbLf)
      Next xv
      
      For i = LBound(y) To UBound(y)
        s = "cmd /c Copy " & y(i) & " " & """" _
          & pPath & "\Master_" & Left(x(i), 8) & ".csv" & """"
        'Debug.Print s
        Shell s, vbHide
      Next i
    End Sub
    
    Function UniqueArray(inArray() As Variant) As Variant
    Dim it As Variant, sn() As Variant, c00 As Variant
      With CreateObject("scripting.dictionary")
          For Each it In inArray()
              c00 = .Item(Left(CStr(it), 8))
          Next
          sn = .Keys ' the array .keys contains all unique keys
          'MsgBox Join(sn, vbLf) ' you can join the array into a string
      End With
      UniqueArray = sn
    End Function
    
    
    Function GetFileList(FileSpec As String) As Variant
    '   Returns an array of filenames that match FileSpec
    '   If no matching files are found, it returns False
    
        Dim FileArray() As Variant
        Dim FileCount As Integer
        Dim FileName As String
        
        On Error GoTo NoFilesFound
    
        FileCount = 0
        FileName = Dir(FileSpec)
        If FileName = "" Then GoTo NoFilesFound
        
    '   Loop until no more matching files are found
        Do While FileName <> ""
            FileCount = FileCount + 1
            ReDim Preserve FileArray(1 To FileCount)
            FileArray(FileCount) = FileName
            FileName = Dir()
        Loop
        GetFileList = FileArray
        Exit Function
    
    '   Error handler
    NoFilesFound:
        GetFileList = False
    End Function
    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