Microsoft 365 Importation fichier texte...

WEIDER

XLDnaute Impliqué
Bonjours à toutes et tous, pouvez-vous m'aider pour cette procédure d'importation car je ne sais pas comment m'y prendre...

Je souhaiterai importer ce fichier texte (voir pièce jointe) dans Excel et via les options d'importation d'Excel, ne garder qu'une seule colonne, composée des :

11,20
11,21
11,20
11,23
...

Autre question, puis-je également changer en même temps le . par une , ?

(Au plus, il peut y avoir 200 lignes par fichier)

Je joins le fichier texte en question.

Mille merci à vous et excellente journée !

Amicalement.
 

Pièces jointes

  • TRACABILITY_REPORT.txt
    9.5 KB · Affichages: 12

kiki29

XLDnaute Barbatruc
Salut, via l'enregistreur de macro , brut de fonderie, tu obtiens déjà ceci, à toi de poursuivre.
VB:
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Downloads\TRACABILITY_REPORT.txt", Destination:=Range("$A$1"))
        .Name = "TRACABILITY_REPORT"
        .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
        .TextFileOtherDelimiter = "|"
        .TextFileColumnDataTypes = Array(1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
 

WEIDER

XLDnaute Impliqué
Je viens de le faire, mais certainement mal car la macro 'bloque'...
bug.JPG


Et comment fais tu une fois la macro enregistrée ? Tu ouvre simplement le fichier texte ?
J'suis une vraie bille.... Désolé.
 

job75

XLDnaute Barbatruc
Bonjour WEIDER, kiki29,

Téléchargez les fichiers joints dans le même dossier (le bureau).

La macro affectée au bouton :
VB:
Sub Resultat()
Dim fichier$, F As Worksheet
fichier = ThisWorkbook.Path & "\TRACABILITY_REPORT.txt"
If Dir(fichier) = "" Then MsgBox "Fichier '" & fichier & "' introuvable !", 48: Exit Sub
Set F = Feuil1 'CodeName, à adapter
Application.ScreenUpdating = False
Workbooks.OpenText fichier, Local:=True 'ouvre le fichier texte
Columns(7).Replace "|*", "", xlPart
F.Range("A2:A" & F.Rows.Count).Delete xlUp 'RAZ
On Error Resume Next 'si aucune SpecialCell
[A12].CurrentRegion.Columns(7).SpecialCells(xlCellTypeConstants, 1).Copy F.[A2]
F.[A:A].NumberFormat = "0.00" 'format nombre à 2 décimales
ActiveWorkbook.Close False 'ferme le fichier texte
End Sub
A+
 

Pièces jointes

  • Résultat(1).xlsm
    18.5 KB · Affichages: 7
  • TRACABILITY_REPORT.txt
    9.5 KB · Affichages: 4

job75

XLDnaute Barbatruc
Une autre manière dans ce fichier (2) avec lecture séquentielle du fichier texte :
VB:
Sub Resultat()
Dim fichier$, col%, x%, texte$, s, a(), n&
fichier = ThisWorkbook.Path & "\TRACABILITY_REPORT.txt"
If Dir(fichier) = "" Then MsgBox "Fichier '" & fichier & "' introuvable !", 48: Exit Sub
col = 5 'colonne du tableau s à traiter, à adapter
x = FreeFile
Open fichier For Input As #x 'ouverture pour lecture séquentielle
While Not EOF(1) 'EndOfFile : fin du fichier
    Line Input #x, texte 'récupère la ligne
    s = Split(texte, "|")
    If UBound(s) >= col Then
        If Val(s(col)) Then
            ReDim Preserve a(n) 'base 0
            a(n) = Val(s(col))
            n = n + 1
        End If
    End If
Wend
Close #x 'fermeture
'---restitution---
With Feuil1 'CodeName de la feuille, à adapter
    If .FilterMode Then .ShowAllData 'si la feuille est filtrée
    With [A2] '1ère cellule de destination, à adapter
        If n Then
            .Resize(n) = Application.Transpose(a) 'Transpose est limitée à 65536 lignes
            .Resize(n).NumberFormat = "0.00" 'format nombre à 2 décimales
        End If
        .Offset(n).Resize(Rows.Count - n - .Row + 1).Delete xlUp 'RAZ au dessous
    End With
    With .UsedRange: End With 'actualise la barre de défilement verticale
End With
End Sub
 

Pièces jointes

  • Résultat(2).xlsm
    20 KB · Affichages: 4
  • TRACABILITY_REPORT.txt
    9.5 KB · Affichages: 3

job75

XLDnaute Barbatruc
Bonjour WEIDER, le forum,

Pour tester j'ai fait passer le tableau du fichier texte de 85 lignes à 51 000 lignes (3,1 Mo).

Chez moi sur Excel 2019 durées d'exécution des macros :

- fichier (1) => 1,1 seconde

- fichier (2) => 0,2 seconde.

A+
 

Discussions similaires

Statistiques des forums

Discussions
311 720
Messages
2 081 915
Membres
101 838
dernier inscrit
Christelle.B86