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
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