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
|
Option Explicit
Sub TestLireFichierTexte()
LireFichierTexte "VIRM.TXT", Sheets("Feuil2") ' Nom de l'onglet à adapter
End Sub
Sub LireFichierTexte(ByVal Fichier As String, ByVal ShTxt As Worksheet)
Dim oFSO As Object, oFl As Object, oTxt As Object, F As Object
Dim I As Integer, DerniereLigne As Long
Dim CheminComplet As String
Dim Aire As Range
ShTxt.UsedRange.Clear
CheminComplet = ActiveWorkbook.Path & "\" & Fichier ' Répertoire à adapter.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFl = oFSO.GetFile(CheminComplet)
Set oTxt = oFl.OpenAsTextStream(ForReading)
With ShTxt
I = 1
While Not oTxt.AtEndOfStream
.Range("A" & I) = oTxt.readline
If I = 1 Then .Range("A" & I) = "000" & Mid(.Range("A" & I), 5)
I = I + 1
Wend
DerniereLigne = .Cells(.Rows.Count, 1).End(xlUp).Row - 2
Set Aire = .Range(.Cells(1, 1), .Cells(DerniereLigne, 1))
End With
Set F = oFSO.CreateTextFile(ActiveWorkbook.Path & "\" & Fichier & " Copie", True)
For I = 1 To Aire.Count
F.write Aire(I).Value & vbNewLine
Next I
F.Close
Set Aire = Nothing
Set oFSO = Nothing: Set oFl = Nothing: Set oTxt = Nothing: Set F = Nothing
End Sub |
Partager