Bonjour,
J'ai crée une macro pour importer un fichier txt nommé PRD. Ce fichier contient des DATA issues d'un système AS400 et reprend le carnet d'adresse client du système d'exploitation. Les données sont inscrites en ligne qu'il faut découper pour obtenir les différents éléments (code site, adresse, CP, Ville etc...).
Cette macro doit permettre de choisir le fichier à importer et doit le découper à largeur fixe pour l'importer sur une nouvelle feuille du classeur XL.
J'ai crée le code suivant :
Sub choisirfichierTXTàimporter()
ChDir "D:\"
Filt = "Fichier Txt (*.txt),*.txt,"
Title = "Selectionnez un Fichier Txt a Importer : "
Filename = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
If Filename = False Then
MsgBox "Aucun fichier choisi"
Exit Sub
End If
' Importer et découper le fichier txt
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;&fichier", Destination:=Range("A1"))
.Name = "fichier"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileFixedColumnWidths = Array(9, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Je pense que le lien entre le choix du fichier sur la 1ère partie de la macro et l'import du fichier txt choisi n'est pas fait. Peut-être même qu'il y a plus simple...
Avez-vous une solution à me proposer ?
En post fichier XL + fichier txt (volontairement réduit à quelques lignes)
Cyrille.
J'ai crée une macro pour importer un fichier txt nommé PRD. Ce fichier contient des DATA issues d'un système AS400 et reprend le carnet d'adresse client du système d'exploitation. Les données sont inscrites en ligne qu'il faut découper pour obtenir les différents éléments (code site, adresse, CP, Ville etc...).
Cette macro doit permettre de choisir le fichier à importer et doit le découper à largeur fixe pour l'importer sur une nouvelle feuille du classeur XL.
J'ai crée le code suivant :
Sub choisirfichierTXTàimporter()
ChDir "D:\"
Filt = "Fichier Txt (*.txt),*.txt,"
Title = "Selectionnez un Fichier Txt a Importer : "
Filename = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
If Filename = False Then
MsgBox "Aucun fichier choisi"
Exit Sub
End If
' Importer et découper le fichier txt
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;&fichier", Destination:=Range("A1"))
.Name = "fichier"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileFixedColumnWidths = Array(9, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Je pense que le lien entre le choix du fichier sur la 1ère partie de la macro et l'import du fichier txt choisi n'est pas fait. Peut-être même qu'il y a plus simple...
Avez-vous une solution à me proposer ?
En post fichier XL + fichier txt (volontairement réduit à quelques lignes)
Cyrille.