how do I concatenate multiple CSV files into one?

  • Thread starter Thread starter RobinC
  • Start date Start date
R

RobinC

I have a bunch of single line CSV files (without even new line characters on
the end) that I want to concatenate into a single file, which I then want to
work with in excel.

How can I do this?
 
Robin,

Put the code below into an otherwise blank workbook, then run the macro, and select the files that
you want to combine when the file selection dialog appears.

HTH,
Bernie
MS Excel MVP


Sub ConsolidateMultipleUserSelectedFiles()
Dim FileArray As Variant
Dim myBook As Workbook
Set myBook = ThisWorkbook

FileArray = Application.GetOpenFilename(MultiSelect:=True)
If IsArray(FileArray) Then
For i = LBound(FileArray) To UBound(FileArray)
Workbooks.OpenText Filename:=FileArray(i), Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(1, 1)
Range("A1").CurrentRegion.Copy myBook.Worksheets(1).Range("a65536").End(xlUp).Offset(1, 0)
ActiveWorkbook.Close False
Next i
End If

myBook.Save
End Sub
 
Will the method outlined in Example 1 deal with putting each line onto a new
row (which is what I *guessed* the OP may have wanted)?
 
Yes if the data is one row in every csv file it is one row in Excel when you import the txt file
 
Just a curiousity question...

If you open a single CSV file, do things get imported correctly.

That lack of linefeed characters scares me.
 
Hi David

No problem here with a new test when I use the exact steps from my site
I go to bed now but maybe Dave or ? can help you

If not I reply tomorrow after work
 
I really think it depends on what this means:
"(without even new line characters on the end)"

I'm not sure if that means that there is no linefeed, no carriage return or
what.

I do remember having to convert text files that came from a UNIX system so that
each line had the DOS CRLF at the end of each line instead of just the UNIX LF
characters.



Hi David

No problem here with a new test when I use the exact steps from my site
I go to bed now but maybe Dave or ? can help you

If not I reply tomorrow after work

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

David Biddulph said:
Interesting!

When I tried it, the resulting file had no new line between the input csv
files, and didn't even have a comma or anything, so the first field of the
second file was concatenated with the last field of the first file, and so
on, and when imported into Excel they were all on the same row..

I wonder what I'm doing different from your suggestion?
--
David Biddulph

Ron de Bruin said:
Yes if the data is one row in every csv file it is one row in Excel when
you import the txt file

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"David Biddulph" <groups [at] biddulph.org.uk> wrote in message
Will the method outlined in Example 1 deal with putting each line onto a
new row (which is what I *guessed* the OP may have wanted)?
--
David Biddulph

Hi RobinC

You can do it also without code if you want
http://www.rondebruin.nl/csv.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


I have a bunch of single line CSV files (without even new line
characters on the end) that I want to concatenate into a single file,
which I then want to work with in excel.

How can I do this?
 
That matches up with my results, too.

I'd add those CRLF characters at the end of each line -- either in each file or
in the way I'd concatenate them.

David said:
In my test file I had no CR, LF, trailing comma, or any other delimiter
after the last character of the data. That was my interpretation of
"(without even new line characters on the end)".
--
David Biddulph

Dave Peterson said:
I really think it depends on what this means:
"(without even new line characters on the end)"

I'm not sure if that means that there is no linefeed, no carriage return
or
what.

I do remember having to convert text files that came from a UNIX system so
that
each line had the DOS CRLF at the end of each line instead of just the
UNIX LF
characters.



Hi David

No problem here with a new test when I use the exact steps from my site
I go to bed now but maybe Dave or ? can help you

If not I reply tomorrow after work

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

"David Biddulph" <groups [at] biddulph.org.uk> wrote in message
Interesting!

When I tried it, the resulting file had no new line between the input
csv
files, and didn't even have a comma or anything, so the first field of
the
second file was concatenated with the last field of the first file, and
so
on, and when imported into Excel they were all on the same row..

I wonder what I'm doing different from your suggestion?
--
David Biddulph

Yes if the data is one row in every csv file it is one row in Excel
when
you import the txt file

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"David Biddulph" <groups [at] biddulph.org.uk> wrote in message
Will the method outlined in Example 1 deal with putting each line
onto a
new row (which is what I *guessed* the OP may have wanted)?
--
David Biddulph

Hi RobinC

You can do it also without code if you want
http://www.rondebruin.nl/csv.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


I have a bunch of single line CSV files (without even new line
characters on the end) that I want to concatenate into a single
file,
which I then want to work with in excel.

How can I do this?
 
Back
Top