XL 2016 Placer des données dans un fichier Excel avec un code VBA

johanvba

XLDnaute Nouveau
Bonjour à tous;

Comment lire à partir d'un fichier texte (.txt) avec son index et placer des données dans un fichier Excel .

voici le code mais n'est pas marché.

VB:
Sub Copy_TXT()
Dim TXT_File As String
Dim AddInName As String
AddInName = "PPR"
TXT_File = ThisWorkbook.Path & "\" & AddInName & ".TXT"
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;TXT_File", Destination:=Range("$N$2"))
        .Name = "PPR"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

merci
 
Solution
Bonjour,

Cela devrait fonctionner ainsi
VB:
Sub Copy_TXT_bis()
Dim TXT_File As String
Dim AddInName As String
AddInName = "PPR"
TXT_File = ThisWorkbook.Path & "\" & AddInName & ".TXT"
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TXT_File, Destination:=Range("$N$2"))
        .Name = "PPR"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850...

Staple1600

XLDnaute Barbatruc
Bonjour,

Cela devrait fonctionner ainsi
VB:
Sub Copy_TXT_bis()
Dim TXT_File As String
Dim AddInName As String
AddInName = "PPR"
TXT_File = ThisWorkbook.Path & "\" & AddInName & ".TXT"
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TXT_File, Destination:=Range("$N$2"))
        .Name = "PPR"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
En tout cas, c'est le cas sur mon PC. ;)
 

Staple1600

XLDnaute Barbatruc
Re

A voir si cette version allégée fonctionne tout aussi bien sur ton PC et avec ton fichier TXT
VB:
Sub Copy_TXT_ter()
Dim TXT_File$, AddInName$
AddInName = "PPR"
TXT_File = ThisWorkbook.Path & "\" & AddInName & ".TXT"
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TXT_File, Destination:=Range("$N$2"))
        .Name = "PPR"
        .FieldNames = True
        .PreserveFormatting = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTabDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
    End With
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
315 136
Messages
2 116 633
Membres
112 819
dernier inscrit
Trivet