Combining csv files

I have been asked to combine several text (comma delimited files) into an Excel Workbook for analysis purposes.  They are all the exact same format with the same headings.  Is there an way to combine all files in a folder?
Who is Participating?
Elton PascuaCommented:
This code should get you started. Save the workbook in the same directory as the CSVs. Record a macro to import one of the files and change in the code accordingly. Also change the path and target of the recorded macro.

Option Explicit

Sub FindCSV()
    Dim strDocPath As String
    Dim strCurrentFile As String
    strDocPath = ThisWorkbook.Path & "\"
    strCurrentFile = Dir(strDocPath & "*.csv*")
    Do While strCurrentFile <> ""
        'Change path and destination accordingly
        With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;" & ThisWorkbook.Path & "\" & strCurrentFile, Destination:=Range("$A1000000").End(xlUp).Offset(1, 0))
                .Name = "file1_1"
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False
                .TextFilePlatform = 437
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = True
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
        strCurrentFile = Dir
End Sub

Open in new window

Dave BaldwinFixer of ProblemsCommented:
Open the first one in a text editor like Notepad.  I would strongly suggest saving it under a new name immediately so you don't lose anything if there is a problem.  Then open the others and copy and paste them into the end of the first / new file and save them.  If they are exactly the same format, they will all load into Excel at once that way.  If for some reason there actually is a difference, then you will still have the original files that you can use.
BBluAuthor Commented:
Thanks, Dave.  I thought of that.  Is that the only way you know of?
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

I would use a combination of DOS command to create a new 'super' file, then open this file in excel and remove the duplicate headers ... see this for instructions

You can use a better directory than c:\ however ... just you the CD command to get to that directory, e.g.
cd c:
cd \path\to\better\directory
Dave BaldwinFixer of ProblemsCommented:
If you only have to do it once, you'd be done by now.  If you have to do it frequently, then a program is better.
Sudhakar KumarManager IT Delivery- II at AonCommented:
You can use simple DOS command to merge multiple csv or text files.

copy *.csv NewCSVFile.csv

Copy all the csv files in a folder and create a batch file copycsv.bat in the same folder and save this command.
BBluAuthor Commented:
Thanks, everyone.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.