Macro VBA choix + import fichier texte à largeur fixe

  • Initiateur de la discussion Initiateur de la discussion THERY
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

THERY

XLDnaute Nouveau
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.
 

Pièces jointes

Re : Macro VBA choix + import fichier texte à largeur fixe

Bonjour,

Je n'ai pas testé mais ceci ne peut fonctionner:

Code:
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;&fichier", Destination:=Range("A1"))


Sortir &fichier de la chaine de caractère:
Code:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Fichier, Destination:=Range("A1"))

A+
 
Re : Macro VBA choix + import fichier texte à largeur fixe

Bonjour,

Voici le code complet.
En bas, aux lignes commentées, adapter éventuellement les types de données, et largeurs de colonnes.

Code vb:
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
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Filename, 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) 'type de données
.TextFileFixedColumnWidths = Array(9, 2) 'largeurs des colonnes
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub





A+
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

  • Question Question
Microsoft 365 CSV en EXCEL
Réponses
1
Affichages
43
Réponses
5
Affichages
410
Réponses
6
Affichages
2 K
Retour