Merge several csv files into one workbook

Justbob

New Member
Joined
May 6, 2015
Messages
30
I have some code that merges several csv files into one worksheet. It works but I found a flaw that needs to be corrected. The csv files I was merging were all comma delimited. Then I ran across a csv that was semi-colon delimited. I recorded a macro to see how that would look and tried to add it to this macro but it does not seem to like it. I found other macros that work but they won't let me select the files like this one, they want to bring in all the files from a single folder. Can someone see what I am doing wrong?

Sub Test_MergeCSVFiles()
Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook
Dim i As String

fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv),*.csv", Title:="Choose Excel files to merge", MultiSelect:=True)

If (vbBoolean <> VarType(fnameList)) Then

If (UBound(fnameList) > 0) Then
countFiles = 0
countSheets = 0

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set wbkCurBook = ActiveWorkbook

i = 1

For Each fnameCurFile In fnameList
countFiles = countFiles + 1

'right after the next step you can see the commented out parts I tried

Set wbkSrcBook = Workbooks.Open(Filename:=fnameCurFile) '_

', Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
'xlDoubleQuote , ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=True, _
'Comma:=True

For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
wksCurSheet.Copy After:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)

i = i + 1
Next

wbkSrcBook.Close SaveChanges:=False

Next

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

On Error Resume Next


MsgBox "Processed " & countFiles & " files" & vbCrLf & "Merged " & countSheets & " worksheets", Title:="Merge Excel files"
End If

Else
MsgBox "No files selected", Title:="Merge Excel files"
End If
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You have a separator ”;”, and in your macro you indicate “Tab” in addition to him. The CONSECTIVEDELIMITER parameter is in your FALSE parameter. May only indicate the separator ";" In True.
 
Upvote 0
This is how it records when opening one csv, somehow I need to open multiple csv's and apply the delimiter. When I cut and paste past the file name and put it after "Set wbkSrcBook = Workbooks.Open(Filename:=fnameCurFile)", its red and won't even run at all.

Sub Macro2()
'
' Macro2 Macro
'

'
Workbooks.OpenText Filename:= _
"C:\test.csv" _
, Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=True, _
Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15 _
, 1), Array(16, 1), Array(17, 1), Array(18, 1)), TrailingMinusNumbers:=True
End Sub
 
Upvote 0
I discovered that csv in different regions use different delimiters.

The files I am getting are UTF-8, from Germany, that is what the Origin:=65001.

Is there a way to detect, in vba, what kind of csv I have and convert it?
 
Upvote 0
Here is a tutorial on how to achieve this with Power Query. No coding required. All achieved in the UI

 
Upvote 0
Once it is set up the Mcode is automated. Any additions or deletions to the folder will automatically update the output.
 
Upvote 0

Forum statistics

Threads
1,226,013
Messages
6,188,421
Members
453,473
Latest member
bbugs73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top