Sub test()
Const Path As String = "C:\Test", Source As String = "fichier test#txt"
Const Champs As String = "" & vbCrLf & _
"DecimalSymbol=," & vbCrLf & _
"Col1=A Text Width 24" & vbCrLf & _
"Col2=B Text Width 14" & vbCrLf & _
"Col3=C Text Width 45" & vbCrLf & _
"Col4=D Text Width 13" & vbCrLf & _
"Col5=E Double Width 8" & vbCrLf & _
"Col6=F TEXT Width 34 " & vbCrLf & _
"Col7=G TEXT Width 4" & vbCrLf
ShemaIni Path, Source, "FixedLength", Champs, False
Dim Cn As Object
Set Cn = CreateObject("ADODB.Connection")
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & Path & "' ;Extended Properties=""text;HDR=no;FMT=Delimited(,)"""
Sheets("Feuil2").Range("A1").CopyFromRecordset Cn.Execute("select A,B,C,D,sum(E),F,G from [" & Source & "] group by A,B,C,D,F,G ")
Cn.Close
MsgBox "Fin"
End Sub
Public Sub ShemaIni(Rep As String, fichier As String, Delimited As String, Optional Champs As String = "", Optional NewCsv As Boolean = False)
Dim txt As String
txt = "[" & Replace(fichier, "#", ".") & "]" & vbCrLf & "Format=" & Delimited
If Champs <> "" Then txt = txt & vbCrLf & Champs
Dim fso, NewFichier
Set fso = CreateObject("Scripting.FileSystemObject")
Set NewFichier = fso.OpenTextFile(Rep & "\schema.ini", 2, True)
NewFichier.Write txt
NewFichier.Close
If NewCsv = True Then
Set NewFichier = fso.OpenTextFile(Rep & "\" & Replace(fichier, "#", "."), 2, True)
NewFichier.Write ""
NewFichier.Close
End If
Set NewFichier = Nothing
Set fso = Nothing
End Sub