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
| Public Sub EclateTempsAuto()
Dim regEx1 As Object
Dim regEx2 As Object
Dim OMatches1, OMatch1, OSubMatch1
Dim OMatches2, OMatch2, OSubMatch2
Dim temps
Dim Ligne As Integer
Dim IndexFichier As Integer
Dim MonFichier As String
Dim ContenuLigne As String
Dim StartCell As Range
Set regEx1 = CreateObject("VBScript.RegExp")
With regEx1
.Global = True
.IgnoreCase = False
' motif avec 3 sous-motifs (entre parenthèses)
' .Pattern = "^(\d{1,4})[ ]+(\d{1,2})[ ]+\d*h*\d+:\d+.\d+[ ]+(\d+)[ ]+([\d:\.]+)[ ]+.[\dh\:\.]+"
.Pattern = "^(\d{1,4})\s(\d{1,2})[ ]+[\d:\.h]+\s(\d+)\s([\d:\.]+)[ ]+.*"
End With
Set regEx2 = CreateObject("VBScript.RegExp")
With regEx2
.Global = True
.IgnoreCase = False
' motif avec 3 sous-motifs (entre parenthèses)
.Pattern = "(\d+)[:\.](\d+)[:\.](\d+)"
End With
Set StartCell = ActiveSheet.Range("A2")
Ligne = 0
MonFichier = "F:\temp\test_excel\R1.txt" '<-- mettez ici le nom du fichier à lire
IndexFichier = FreeFile()
Open MonFichier For Input As #IndexFichier 'ouvre le fichier
While Not EOF(IndexFichier) '
Line Input #IndexFichier, ContenuLigne ' lecture du fichier ligne par ligne: la variable "ContenuLigne" contient le contenu de la ligne active
' Debug.Print ContenuLigne
' On recherche tous les éléments
Set OMatches1 = regEx1.Execute(ContenuLigne)
For Each OMatch1 In OMatches1
If OMatch1.SubMatches.Count = 4 Then
Set OMatches2 = regEx2.Execute(OMatch1.SubMatches(3))
If OMatches2.Count = 1 Then
If OMatches2(0).SubMatches.Count = 3 Then
temps = OMatches2(0).SubMatches(0) * 60 + OMatches2(0).SubMatches(1) + OMatches2(0).SubMatches(2) / 1000
StartCell.Offset(Ligne, 0) = OMatch1.SubMatches(1)
StartCell.Offset(Ligne, 1) = OMatch1.SubMatches(0)
StartCell.Offset(Ligne, 2) = OMatch1.SubMatches(2)
StartCell.Offset(Ligne, 3).Value2 = temps
Ligne = Ligne + 1
End If
End If
' Debug.Print Temps
' Debug.Print "=================="
End If
Next
Wend
Close #IndexFichier ' ferme le fichier
Set regEx1 = Nothing
Set regEx2 = Nothing
Debug.Print "Fin"
End Sub |
Partager