Вы ничего не говорите, и я должен покинуть офис ... Пожалуйста, проверьте следующий код. Предполагается, что мои вышеупомянутые предположения (в комментарии) верны. Код прокомментирован таким образом, чтобы вы поняли его значение. Он использует стандартные функции VBA / VBScript. Никаких вызовов API и файлов bat ... Это исключает первую строку csv из процесса слияния.
Код прокомментирован таким образом, чтобы его было легко понять. Если что-то непонятно, не стесняйтесь обращаться за разъяснениями!
Sub testMeergeCSVFiles()
Dim fullFilename As String, objFSO As Object, objTF As Object, arrIn As Variant
Dim masterFullName As String, i As Long, strIn As String, strExt As String
Dim finStr As String, foldName As String, wb As Workbook, xlsFullName As String
masterFullName = Environ("TEMP") & "\" & Format(Now, "dd-mm-yy-h-mm-ss") & ".csv"
xlsFullName = Application.DefaultFilePath & "\" & "MasterCSV " & _
Format(Now, "dd-mmm-yyyy h-mm-ss") & ".xlsx"
foldName = GetFolderPath(ThisWorkbook.path & "\") ' it uses GetFolderPath function, starting from
' ThisWorkbook path
fullFilename = Dir(foldName & "\" & "*.csv") 'iterate in folder for csv files
Do While fullFilename <> ""
'If left(fullFilename, 9) = "TestMerge" Then 'only for testing reason (for me)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile(foldName & "\" & fullFilename, 1)
strIn = objTF.ReadAll 'extract all the content of the file
objTF.Close
arrIn = Split(strIn, vbLf): sep = vbLf ' put the strinig in an array
'start of modification__________________________________________
If UBound(arrIn) < 1 Then arrIn = Split(strIn, vbCr): sep = vbCr
If UBound(arrIn) < 1 Then arrIn = Split(strIn, vbCrLf): sep = vbCr
Debug.Print "Sep: " & Len(sep), Asc(sep) ' adapted here...
'end of modification____________________________________________
For i = 1 To UBound(arrIn) ' build the string which excepts first row
If arrIn(i) <> vbLf Then strExt = strExt & arrIn(i)
Next i
If finStr = "" Then 'Build the final string to be loaded in Master CSV
finStr = strExt
Else
finStr = finStr & strExt
End If
'End If
strExt = ""
fullFilename = Dir 'reinitialize the loop
Loop
If finStr = "" Then Exit Sub 'happening in case of no csv files found in the folder
Open masterFullName For Output As #1
Print #1, finStr 'dropping the string in the master csv file
Close #1
'Opening the master file in Excel:
Workbooks.OpenText fileName:=masterFullName, origin:=xlWindows, StartRow _
:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True, _
Space:=False, Other:=False
'Save csv file As Excel xlsx format:
Set wb = ActiveWorkbook
wb.SaveAs fileName:=xlsFullName, FileFormat:=xlOpenXMLWorkbook
Kill masterFullName
MsgBox "MasterCSV Excel file saved as " & xlsFullName
End Sub
Private Function GetFolderPath(Optional strPath As String) As String
Dim fldr As FileDialog, sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.buttonName = "Select Folder"
.Title = "Select .CSV files to be processed Folder"
.AllowMultiSelect = False
If strPath <> "" Then .InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolderPath = sItem
Set fldr = Nothing
End Function