1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
Sub ImportFichierTexte()
Dim Fichier As String
Dim ShImport As Worksheet, ShMaj As Worksheet
Dim I As Long, J As Long, DerniereLigne As Long
Dim MonTableau As Variant
'chemin à adapter
Fichier = ActiveWorkbook.Path & "\Fichier source.txt"
Set ShImport = Sheets("Feuil1")
Set ShMaj = Sheets("Feuil2")
With ShMaj
.Cells.Clear
.Range(.Cells(1, 1), .Cells(1, 6)) = Array("Date", "Heures", "Module", "Adresse", "Direction", "Texte")
End With
With ShImport
.UsedRange.Clear
.QueryTables.Add("TEXT;" & Fichier, ShImport.[A1]).Refresh
DerniereLigne = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
J = 3
With ShImport
For I = 1 To DerniereLigne Step 3
MonTableau = Split(.Cells(I, 1).Value, Chr(32))
Debug.Print .Cells(I, 1) & " : " & UBound(MonTableau)
ShMaj.Cells(J, 1) = MonTableau(2)
ShMaj.Cells(J, 2) = MonTableau(3)
ShMaj.Cells(J, 3) = Split(MonTableau(4), ".")(0)
ShMaj.Cells(J, 4) = Split(MonTableau(4), ".")(1)
ShMaj.Cells(J, 5) = Split(MonTableau(4), ".")(2)
ShMaj.Cells(J, 6) = .Cells(I + 1, 1).Value
J = J + 1
Next
End With
With ShMaj
With .UsedRange
.EntireColumn.AutoFit
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
With .Rows(2)
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
End With
End With
.Range(.Cells(1, 1), .Cells(1, 5)).EntireColumn.HorizontalAlignment = xlCenter
End With
Set ShImport = Nothing
Set ShMaj = Nothing
End Sub |
Partager