+ Reply to Thread
Results 1 to 8 of 8

Merge select CSV / txt files

  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    Europe
    MS-Off Ver
    Excel 2013/365
    Posts
    24

    Merge select CSV / txt files

    Hi,

    Looking to create a .csv file merging macro that can merge files selected by an Excel user into one combined single-sheet Excel document.
    In my use case whatever .csv files selected by the user will all have the same set of columns with one header row in each one.

    In the past I've used a customized version of the "Merging a Range from Selected Workbooks" from this knowledge page: (https://msdn.microsoft.com/en-us/lib....aspx#Anchor_3
    But it's had a few quirks:
    • It doesn't account for a header row in first file selected.
    • It applies some formatting to columns (specifically noticeable with columns containing dates/times e.g. "dd/mm/yyyy hh:mm:ss" whereby the resulting combined file has lost the date information for a "Custom" format).

    I recently stumbled upon this macro which seems promising on this page (Merge list of csv, txt files) (http://analystcave.com/merge-csv-fil...s-in-a-folder/).
    But it doesn't fit my use case completely, so I'm looking for help with updating it:
    • It is hard-coded which are the input and output files. I'd like the input files to be selected via Application.GetOpenFilename instead and the output to simply be left Open at the end (leaving it to the user to save it).
    • It checks for a header row in the first input file but still leaves blank rows in the output file (where the headers would have been for subsequent merged files). There should be no empty rows in between merged files in the output.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    It sounds like it should be an easy quick fix but I've been staring myself blind for a while now, any help is much appreciated.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Merge select CSV / txt files

    Have you tried Ron's free RDBMerge add-in? I think it will do what you want.

    RDBMerge, Excel Merge Add-in
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-02-2012
    Location
    Europe
    MS-Off Ver
    Excel 2013/365
    Posts
    24

    Re: Merge select CSV / txt files

    Hi, I have looked at his add-in in the past, the older script cited on the Microsoft knowledge page was written by RDB and uses as far as I understand much of the same code underneath.
    Unfortunately I can't use external add-ins in this case in the event the routine needs to be called from another process.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Merge select CSV / txt files

    Try this.

    You still may have issues with custom date formats. CSV files don't have serial dates. They have text. When Excel reads a text-date from a text file, it attempts to convert it to a serial-date (a numeric value) which then you can apply any date format you like. If Excel doesn't convert the text-date to a serial-date, it may look like the original text from the CSV file, but Excel only sees it as text and not as a serial-date.


    Please Login or Register  to view this content.

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

    Re: Merge select CSV / txt files

    That routine does add an extra blank line. See if this change helps.
    Please Login or Register  to view this content.
    You will need a slight tweak for #4. I do like that dialog better than GetFileOpenFilename().
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 11-02-2016 at 12:51 PM.

  6. #6
    Registered User
    Join Date
    08-02-2012
    Location
    Europe
    MS-Off Ver
    Excel 2013/365
    Posts
    24

    Re: Merge select CSV / txt files

    Hi, I haven't had the chance to test out your suggestions yet. Will do shortly, but would it be possible to adjust it so the macro does not save an output "Merged.csv" file and instead make use of an unsaved temp workbook as the output that is left open to the user at the end of the sub?

  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: Merge select CSV / txt files

    An advantage to the method that you posted is that it works on closed files in the background so it is very fast.

    I showed you how to open the created file in the last line of code. If you want to rename the CSV created file, then that is easily done in the macro. Once open, if you really wanted to copy that sheet to another open workbook, that is easily done too. The syntax for that is easily seen by a recorded macro.
    Last edited by Kenneth Hobson; 11-03-2016 at 01:30 PM.

  8. #8
    Registered User
    Join Date
    08-02-2012
    Location
    Europe
    MS-Off Ver
    Excel 2013/365
    Posts
    24

    Re: Merge select CSV / txt files

    Hi, sorry let me clarify. What I'm looking for it not to have any "Merged.csv" output file saved at all, but to simply put the selected merged files' data in an opened Excel workbook as part of the sub (via Workbooks.Add(xlWBATWorksheet).Worksheets(1) as example) and present the merged data to the user at the end in that sheet. Does that make sense?

+ 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. Need code to select files in a folder to merge PDF using adobe acrobat X
    By karel85 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-15-2014, 04:19 PM
  2. Merge .CSV files
    By tapsmiled in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-12-2014, 10:43 PM
  3. [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
  4. I can't select a range and then merge it in UDF.
    By Francisco Sousa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2013, 12:46 PM
  5. Merge 2 XLS files into 1
    By vbjohn in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-13-2007, 02:59 PM
  6. [SOLVED] Merge txt files
    By Ronny in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2006, 11:10 AM
  7. [SOLVED] Select Merge Areas
    By al in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-07-2005, 10:45 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