3

I have ~100 CSV files that I want to combine into one single Excel spreadsheet (everything in one tab, not in separate tabs). These CSV files are identical in format: each contains 4,000 rows and 2 columns with header, about 60 KB in size.

All solutions I've found so far append the next CSV at the end of the last row of the active tab. I haven't seen one that can append new CSVs to the columns immediate to the right of last column.

Here're some examples.

  1. DOS copy method: copy *.csv merge.txt

  2. This solution. (VBA script.)

  3. Using Excel's Data>New Query>From File>From Folder (Excel 2013) .

There are also examples with Windows Powershell scripts to merge CSV files on the internet.

All of the above create a single Excel spreadsheet with about 400,000 rows of data, which is useless to me.

I'd appreciate suggestions to solve this problem. Thanks!

Edit. An easy solution is found: using r's cbind() to combine the data in a data frame and then write it to a csv. The entire process took ~3 seconds. Right tool for the job!

Thanks to everyone for contributing. Cheers,

--tcollar

6
  • tcollar - If I had time to play with it more I would to confirm the exact syntax and to keep it as simple as possible logic wise but I don't right now. Look over this post and play with some of these variations of the PowerShell logic.... social.technet.microsoft.com/Forums/scriptcenter/en-US/… Commented Sep 24, 2019 at 1:34
  • I haven't seen one that can append new CSVs to the columns immediate to the right of last column. Consolidate data in multiple worksheets
    – Akina
    Commented Sep 24, 2019 at 5:11
  • Your issue is contradicts your need,,, you got solution and unable to alter it as your need,,, I think your basic need is append CSV file data side by side !! Commented Sep 24, 2019 at 7:18
  • This VBA code works,, nextColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column + 1 Commented Sep 24, 2019 at 8:04
  • support.office.com/en-us/article/…
    – Lee
    Commented Sep 24, 2019 at 8:50

3 Answers 3

1

Not sure about "native" Windows 10, but if you have the Windows Subsystem for Linux installed, you can use the UNIX paste command, which should concatenate the files by columns, in the way you are asking.

4
  • 1
    In addition to paste the OP may want to read the man page for join as well.
    – ivanivan
    Commented Sep 24, 2019 at 0:03
  • Neither is possible. The CSV files are business documents. Can't be transferred outside the work. I'm using Linux Mint at home.
    – tcollar
    Commented Sep 24, 2019 at 0:17
  • @tcollar Too bad. To be clear, Windows Subsystem for Linux installs onto Windows 10. It's not possible to use this within your business environment?
    – Scot
    Commented Sep 24, 2019 at 0:24
  • I know. I've checked. It is not installed for sure. I suppose I can ask for it. But it's going to be a pain.
    – tcollar
    Commented Sep 24, 2019 at 0:29
0

Using your third option: 'Using Excel's Data>New Query>From File>From Folder (Excel 2013)'

Click on the transform data option, this opens the power query editor. It will show a list of the files.

Click on the button in the header of the column content to create a function that can transform every individual CSV file before appending them.

Select the right table or worksheet and do the following in the transform sample file function.

  1. Insert the column names into the first row by using `use headers as first row'
  2. Transpose Table

Then in the query where the function is invoked, the transposed CSV files are appended. Simply transpose the table back to original shape and promote the first row to headers.

3
  • I tried it. It sorta worked, but on my computer it took more than 10 minutes just to combine the transposed data sets!
    – tcollar
    Commented Sep 24, 2019 at 23:00
  • Glad to hear that it worked
    – Burd
    Commented Sep 25, 2019 at 5:29
  • @Burd This is exactly what I want to do, but all it does is add all the data below each other (data from each csv column is added below in the same column as that of previous csv), and transpose it you get the same thing but transposed? DO you know what I'm doing wrong? Thanks Commented Nov 24, 2020 at 20:58
0

Here's the r code to combine csv files by column:

setwd("C:/mycsvdir")
filenames <- list.files(full.name=TRUE)

## read csv, skipping the first two rows of every file
Everything <- lapply(filenames, function(i){read.csv(i, header=TRUE, skip=2)}
combined.df <- do.call(cbind.data.frame, Everything)

write.csv(combined.df, file = "combined.csv", row.names=FALSE)

If you want to combine csv files by row, just replace cbind with rbind.

Edit: About the sequence of merging: my csv files are named like mycsv 001.csv, mycsv 002.csv, etc. They were read in and merged exactly in that sequence, which was great for my purpose.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .