Bonjour
Voici mon problème, je souhaite importer des fichiers .txt dans excel, un fichier par page excel.
Voici la VBA que j'utilise :
Sub insertion_txt_auto()
Dim Fso As Object
Dim FsoRepertoire As Object
Dim FsoFichier As Object
Dim str() As String
Dim strLigne As String
Dim i As Long
Dim c As Integer
Set Fso = CreateObject("Scripting.FileSystemObject")
Set FsoRepertoire = Fso.GetFolder("H:\These\manip\TEOS_EtOH_TEP_ABTES\vba")
For h = 1 To 22
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "TEOS_EtOH_TEP_ABTES_" & h
Next h
h = 1
'Boucle sur fichiers du repertoire
iCopie = 2
For Each FsoFichier In FsoRepertoire.Files
str = Split(FsoFichier.Name, ".")
If str(UBound(str)) = "txt" Then
Open FsoFichier For Input As #1
i = 1
Do While Not EOF(1)
Line Input #1, strLigne
Sheets("TEOS_EtOH_TEP_ABTES_" & h).Select
Cells(i, 1).Value = strLigne
i = i + 1
Loop
h = h + 1
Close #1
End If
Next
End Sub
Le problème avec cette VBA, c'est que tout mon ficher .txt est inséré dans une seule colonne, or je souhaiterai avoir une colonne du txt = à une colonne excel.
Si je fais la réalisation de macro automatique pour importer correctement le fichier txt j'obtiens ceci :
Sub insertion_txt_manuelle()
'
' Macro2 Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;H:\These\manip\TEOS_EtOH_TEP_ABTES\test.txt", Destination:=Range("$A$1" _
))
.Name = "test"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Mais j'arrive pas à croiser les deux VBA.
Je vous joins un PJ un exemple de fichier txt
En vous remerciant.