+ Reply to Thread
Results 1 to 9 of 9

Automatically Combine a Folder of CSV Files.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-14-2019
    Location
    Coventry, England
    MS-Off Ver
    2010
    Posts
    23

    Red face Automatically Combine a Folder of CSV Files.

    Afternoon All,

    I have just signed up today so any help is much appreciated and i will help in return where possible.

    The issue i am having is i have an old machine that im using to gauge parts, every reading it outputs as a .CSV file.

    So after 20 parts i have 20 separate .CVS files with the same format just one line of Data.

    Is it possible to automatically merge these files into one Excel file?
    I have seen a method but it seems long winded and i was just wondering if there was a way of doing it automatically.

    I was hoping to create a live document if possible so every time a new part is gauged it automatically gets added to the file.

    Please see picture below, showing an example of the filenames.

    Any help with this is greatly appreciated.

    Many thanks in advance.

    Dale

    snipex.PNG

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatically Combine a Folder of CSV Files.

    There are number of ways to do it.

    But you'll need to provide sample csv and how it should be combined.

    To upload file, use "Go Advanced" button and follow "Manage Attachments" hyperlink. It will launch new tab/window for managing uploads.

    Several methods.
    1. Get PowerQuery add-in from MS. And use binary combine.
    2. Use VBA. Using Dir function to iterate through files, open and copy range.
    3. Use VBA. Using ADODB and SQL query to combine CSV. This is bit complex if you are not used to it, and will require schema.ini file to be created.
    etc.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    03-14-2019
    Location
    Coventry, England
    MS-Off Ver
    2010
    Posts
    23

    Re: Automatically Combine a Folder of CSV Files.

    Many thanks for the quick reply, i have attached an example of the .CSV file so you can see the format.

    Basically i just want the individual records to be into one table so i can do Live SPC, same layout just the records on the following rows.

    From your list of options would any allow automatic merging? so say when a macro button is pressed it adds any additional .CSV files that have been added.

    obviously when a new part is gauged i can easily update a graph without having to manually add the new data every time.

    I hope you can understand what i mean im not best with wording haha.

    Many thanks
    Attached Files Attached Files

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatically Combine a Folder of CSV Files.

    Need bit more info. How should the combined/merged data look like?

    Just flat table with exactly same headers, and any additional data (file) added below?

  5. #5
    Registered User
    Join Date
    03-14-2019
    Location
    Coventry, England
    MS-Off Ver
    2010
    Posts
    23

    Re: Automatically Combine a Folder of CSV Files.

    Yeah the same headers as the attached .CSV file with the additional data on the rows descending.

    Many thanks for your help.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatically Combine a Folder of CSV Files.

    If using PowerQuery method follow instruction in the link.
    https://www.myonlinetraininghub.com/...-from-a-folder

    For PQ Add-in download follow link below.
    https://www.microsoft.com/en-ca/down....aspx?id=39379

    If using VBA method. See below for sample code using variation of method 2 in post #2.
    This assumes that you have all csv to be imported in same folder. Change fPath as needed.
    See sample attached.
    Sub Demo()
    Dim fPath As String: fPath = "C:\Test\CSV" & "\" 'Change path as needed
    Dim fExt As String: fExt = "*.CSV"
    Dim mFile As String, fColl As New Collection
    Dim intFF As Integer: intFF = FreeFile()
    Dim i As Integer, j As Integer
    mFile = Dir(fPath & fExt)
    Do While Len(mFile) > 0
        fColl.Add fPath & mFile
        mFile = Dir()
    Loop
    j = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To fColl.Count
        Open fColl(i) For Input As #intFF
        Do Until EOF(intFF)
            Line Input #intFF, ReadData
            If InStr(ReadData, "Date and time") Then
            Else
                j = j + 1
                Cells(j, 1).Resize(, 53) = Split(ReadData, ",")
            End If
        Loop
        Close #intFF
    Next
    
    End Sub
    Note: Depending on your process, this requires additional coding. My recommendation is to move files to another folder, once imported.
    Attached Files Attached Files
    Last edited by CK76; 03-14-2019 at 12:21 PM.

  7. #7
    Registered User
    Join Date
    03-14-2019
    Location
    Coventry, England
    MS-Off Ver
    2010
    Posts
    23

    Re: Automatically Combine a Folder of CSV Files.

    Morning CK76,

    Many thanks, your VBA option works perfectly.

    I added a little so on open it deletes the data and merges capturing any new data.

    Just a small one is there a way to slightly adjust the VBA a little to get it rename a few columns headers?

    i didn't notice at first but some of the header the machine generates are confusing.

    MANY MANY Thanks for your help with this though

    Dale J



    Private Sub Workbook_Open()
    
    Range("A2:AQ99999").ClearContents
    
    Dim fPath As String: fPath = "I:\MARPOSSGAUGEDATA" & "\" 'Change path as needed
    Dim fExt As String: fExt = "*.CSV"
    Dim mFile As String, fColl As New Collection
    Dim intFF As Integer: intFF = FreeFile()
    Dim i As Integer, j As Integer
    mFile = Dir(fPath & fExt)
    Do While Len(mFile) > 0
        fColl.Add fPath & mFile
        mFile = Dir()
    Loop
    j = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To fColl.Count
        Open fColl(i) For Input As #intFF
        Do Until EOF(intFF)
            Line Input #intFF, ReadData
            If InStr(ReadData, "Date and time") Then
            Else
                j = j + 1
                Cells(j, 1).Resize(, 53) = Split(ReadData, ",")
            End If
        Loop
        Close #intFF
    Next
    
    End Sub
    Last edited by alansidman; 03-15-2019 at 03:13 AM.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,740

    Re: Automatically Combine a Folder of CSV Files.

    Dale

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Automatically Combine a Folder of CSV Files.

    Just a small one is there a way to slightly adjust the VBA a little to get it rename a few columns headers?
    Actually, code isn't doing anything with headers. Since headers will be constant. I skip them in import process.
    It's just manual copy and paste from source csv file. You can change it to anything you want, and it won't impact the code.
    Last edited by CK76; 03-15-2019 at 08:42 AM.

+ 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] Combine ranges A1:B100 for all xlsx files in a folder into a new file
    By billj in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-01-2017, 08:49 PM
  2. Combine and Loop Macros on all files in folder
    By BossaNovaMSC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2015, 07:41 PM
  3. Mac macro to combine all excel files in a folder
    By koosh1986 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-26-2015, 10:54 PM
  4. Continuously combine all excel files in a folder
    By koosh1986 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-23-2015, 07:32 PM
  5. Open Folder Dialog Box with Combine Files Code
    By wealthistime in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2015, 08:47 PM
  6. Combine Data from Closed Files within the same folder
    By lasjbp9 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-07-2013, 10:07 AM
  7. Folder Dialog Box with Combine Files Code
    By wealthistime in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2011, 05:43 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