IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

lecture fichier plat TXT sans espace dans excel


Sujet :

Macros et VBA Excel

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2011
    Messages : 75
    Points : 48
    Points
    48
    Par défaut lecture fichier plat TXT sans espace dans excel
    Bonjour,

    Je cherche à ouvrir dans Excel, de manière automatisée, un fichier TXT ayant 30 colonnes, chacune des colonnes ayant un nb de caractères spécifique. Bien entendu je connais le nombre de caractères alloués à chaque colonne.

    Pourriez-vous me guider sur la macro à effectuer afin d'y arriver ?

    Je sais pour l'instant réaliser l'inverse (de excel à texte avec nb carac délimités)
    Dans cet exemple j'avais 55 colonnes :


    Voici le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    Sub XlsTOTxt()
     
    Dim i As Long
     
    Open "C:\Users\Julien Arnault\Desktop\texte.txt" For Output As #1
     
     
    Sheets("1").Select
    For i = 2 To Range("A:A").End(xlDown).Row
     
                    Print #1, Left(Range("A" & i).Value, 10) & Space(10 - Len(Left(Range("A" & i).Value, 10))) & Left(Range("B" & i).Value, 14) & Space(14 - Len(Left(Range("B" & i).Value, 14))) & Left(Range("C" & i).Value, 13) & Space(13 - Len(Left(Range("C" & i).Value, 13))) & _
                        Left(Range("D" & i).Value, 9) & Space(9 - Len(Left(Range("D" & i).Value, 9))) & Left(Range("E" & i).Value, 6) & Space(6 - Len(Left(Range("E" & i).Value, 6))) & Left(Range("F" & i).Value, 30) & Space(30 - Len(Left(Range("F" & i).Value, 30))) & _
                        Left(Range("G" & i).Value, 4) & Space(4 - Len(Left(Range("G" & i).Value, 4))) & Left(Range("H" & i).Value, 1) & Space(1 - Len(Left(Range("H" & i).Value, 1))) & Left(Range("I" & i).Value, 30) & Space(30 - Len(Left(Range("I" & i).Value, 30))) & _
                        Left(Range("J" & i).Value, 20) & Space(20 - Len(Left(Range("J" & i).Value, 20))) & Left(Range("K" & i).Value, 10) & Space(10 - Len(Left(Range("K" & i).Value, 10))) & Left(Range("L" & i).Value, 13) & Space(13 - Len(Left(Range("L" & i).Value, 13))) & _
                        Left(Range("M" & i).Value, 1) & Space(1 - Len(Left(Range("M" & i).Value, 1))) & Left(Range("N" & i).Value, 2) & Space(2 - Len(Left(Range("N" & i).Value, 2))) & Left(Range("O" & i).Value, 1) & Space(1 - Len(Left(Range("O" & i).Value, 1))) & _
                        Left(Range("P" & i).Value, 1) & Space(1 - Len(Left(Range("P" & i).Value, 1))) & Left(Range("Q" & i).Value, 10) & Space(10 - Len(Left(Range("Q" & i).Value, 10))) & Left(Range("R" & i).Value, 10) & Space(10 - Len(Left(Range("R" & i).Value, 10))) & _
                        Left(Range("S" & i).Value, 1) & Space(1 - Len(Left(Range("S" & i).Value, 1))) & Left(Range("T" & i).Value, 10) & Space(10 - Len(Left(Range("T" & i).Value, 10))) & Left(Range("U" & i).Value, 10) & Space(10 - Len(Left(Range("U" & i).Value, 10))) & _
                        Left(Range("V" & i).Value, 10) & Space(10 - Len(Left(Range("V" & i).Value, 10))) & Left(Range("W" & i).Value, 10) & Space(10 - Len(Left(Range("W" & i).Value, 10))) & Left(Range("X" & i).Value, 13) & Space(13 - Len(Left(Range("X" & i).Value, 13))) & _
                        Left(Range("Y" & i).Value, 10) & Space(10 - Len(Left(Range("Y" & i).Value, 10))) & Left(Range("Z" & i).Value, 10) & Space(10 - Len(Left(Range("Z" & i).Value, 10))) & Left(Range("AA" & i).Value, 10) & Space(10 - Len(Left(Range("AA" & i).Value, 10))) & _
                        Left(Range("AB" & i).Value, 10) & Space(10 - Len(Left(Range("AB" & i).Value, 10))) & Left(Range("AC" & i).Value, 3) & Space(3 - Len(Left(Range("AC" & i).Value, 3))) & Left(Range("AD" & i).Value, 10) & Space(10 - Len(Left(Range("AD" & i).Value, 10))) & _
                        Left(Range("AE" & i).Value, 13) & Space(13 - Len(Left(Range("AE" & i).Value, 13))) & Left(Range("AF" & i).Value, 8) & Space(8 - Len(Left(Range("AF" & i).Value, 8))) & Left(Range("AG" & i).Value, 13) & Space(13 - Len(Left(Range("AG" & i).Value, 13))) & _
                        Left(Range("AH" & i).Value, 3) & Space(3 - Len(Left(Range("AH" & i).Value, 3))) & Left(Range("AI" & i).Value, 3) & Space(3 - Len(Left(Range("AI" & i).Value, 3))) & Left(Range("AJ" & i).Value, 1) & Space(1 - Len(Left(Range("AJ" & i).Value, 1))) & _
                        Left(Range("AK" & i).Value, 1) & Space(1 - Len(Left(Range("AK" & i).Value, 1))) & Left(Range("AL" & i).Value, 1) & Space(1 - Len(Left(Range("AL" & i).Value, 1))) & Left(Range("AM" & i).Value, 1) & Space(1 - Len(Left(Range("AM" & i).Value, 1))) & _
                        Left(Range("AN" & i).Value, 10) & Space(10 - Len(Left(Range("AN" & i).Value, 10))) & Left(Range("AO" & i).Value, 10) & Space(10 - Len(Left(Range("AO" & i).Value, 10))) & Left(Range("AP" & i).Value, 1) & Space(1 - Len(Left(Range("AP" & i).Value, 1))) & _
                        Left(Range("AQ" & i).Value, 11) & Space(11 - Len(Left(Range("AQ" & i).Value, 11))) & Left(Range("AR" & i).Value, 11) & Space(11 - Len(Left(Range("AR" & i).Value, 11))) & Left(Range("AS" & i).Value, 11) & Space(11 - Len(Left(Range("AS" & i).Value, 11))) & _
                        Left(Range("AT" & i).Value, 13) & Space(13 - Len(Left(Range("AT" & i).Value, 13))) & Left(Range("AU" & i).Value, 13) & Space(13 - Len(Left(Range("AU" & i).Value, 13))) & Left(Range("AV" & i).Value, 10) & Space(10 - Len(Left(Range("AV" & i).Value, 10))) & _
                        Left(Range("AW" & i).Value, 5) & Space(5 - Len(Left(Range("AW" & i).Value, 5))) & Left(Range("AX" & i).Value, 5) & Space(5 - Len(Left(Range("AX" & i).Value, 5))) & Left(Range("AY" & i).Value, 10) & Space(10 - Len(Left(Range("AY" & i).Value, 10))) & _
                        Left(Range("AZ" & i).Value, 1) & Space(1 - Len(Left(Range("AZ" & i).Value, 1))) & Left(Range("BA" & i).Value, 10) & Space(10 - Len(Left(Range("BA" & i).Value, 10))) & Left(Range("BB" & i).Value, 10) & Space(10 - Len(Left(Range("BB" & i).Value, 10))) & _
                        Left(Range("BC" & i).Value, 8) & Space(8 - Len(Left(Range("BC" & i).Value, 8)))
    Next i
     
    Close #1
     
    End Sub
    Merci à vous,

    Julien

  2. #2
    Expert éminent sénior
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Points : 11 274
    Points
    11 274
    Par défaut
    Salut,consulte http://warin.developpez.com/access/fichiers/#LII-B-1
    avec des instructions du genre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Trim$(Mid$(sChaine, 9, 28))
    ,tu devrais y arriver , l'usage de [F1] n'est pas prohibé

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2011
    Messages : 75
    Points : 48
    Points
    48
    Par défaut
    Bonjour et merci de la réponse. Je consulte régulièrement l'aide proposée par Excel, cependant ici, je veux délimiter des caractères, et automatiser la chose.

    Ceci n'est pas expliqué dans l'aide d'excel, d'où ma question ...

  4. #4
    Expert éminent sénior
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Points : 11 274
    Points
    11 274
    Par défaut
    Salut,tu sembles être dans une position attentiste ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    Option Explicit
     
    Sub Sel()
    Dim Fichier As Variant
        ChDir ThisWorkbook.Path
        Fichier = Application.GetOpenFilename("Fichier Txt (*.txt), *.txt")
        If Fichier <> False Then Lire (Fichier)
    End Sub
     
    Private Sub Lire(sNomFichier As String)
    Dim sChaine As String
    Dim iRow As Long, iCol As Long
    Dim NumFichier As Integer
     
        Application.ScreenUpdating = False
        ShTst.Cells.Clear
     
        Close
        NumFichier = FreeFile
     
        iRow = 0
        Open sNomFichier For Input As #NumFichier
            Do While Not EOF(NumFichier)
                iCol = 1: iRow = iRow + 1
                Line Input #NumFichier, sChaine
                With ShTst
                    .Cells(iRow, iCol) = Mid$(sChaine, 1, 9)
                    iCol = iCol + 1
                    .Cells(iRow, iCol) = Mid$(sChaine, 10, 10)
                    iCol = iCol + 1
                    .Cells(iRow, iCol) = Mid$(sChaine, 20, 10)
                    iCol = iCol + 1
                    .Cells(iRow, iCol) = Mid$(sChaine, 30, 2)
                End With
            Loop
        Close #NumFichier
     
        Application.ScreenUpdating = True
    End Sub
    pour un fichier texte du genre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    12345678910111213141516171819AB
    AB345678910111213141516171819AB
    12345678910111213141516171819AB
    CD345678910111213141516171819AB
    12345678910111213141516171819AB
    EF345678910111213141516171819AB
    On obtient ceci dans Excel

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    123456789	1011121314	1516171819	AB
    AB3456789	1011121314	1516171819	AB
    123456789	1011121314	1516171819	AB
    CD3456789	1011121314	1516171819	AB
    123456789	1011121314	1516171819	AB
    EF3456789	1011121314	1516171819	AB

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2011
    Messages
    75
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Mai 2011
    Messages : 75
    Points : 48
    Points
    48
    Par défaut
    vous êtes bien froid ...

    J'ai néanmoins passé la journée à coder, et j'obtiens le résultat attendu :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    Sub MaMacro()
        ActiveWorkbook.Worksheets.Add
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:Mondoc.txt", Destination:=Range("$A$1"))
            .Name = "Onglet 1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 1252
            .TextFileStartRow = 1
            .TextFileParseType = xlFixedWidth
            .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, 1, 1, 1, 1, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileFixedColumnWidths = Array(8, 4, 30, 9, 13, 14, 4, 1, 1, 1, 1, 4, 8, 13, 2, 30, 30, 5, 7, 14, 14, 7, 14, 1, 14, 14, 1, 8, 1, 8)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub
    Merci à kiki29 malgré tout,

    A bientôt !

  6. #6
    Expert éminent sénior
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Points : 11 274
    Points
    11 274
    Par défaut
    Re,tant qu'à faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Option Explicit
     
    Sub SelFichier()
    Dim Fichier As Variant
        ChDir ThisWorkbook.Path
        Fichier = Application.GetOpenFilename("Fichier Txt (*.txt), *.txt")
        If Fichier <> False Then Import (Fichier)
    End Sub
     
    Private Sub Import(sFichier As String)
        Feuil1.Cells.Clear
        With Feuil1.QueryTables.Add(Connection:= _
            "TEXT;" & sFichier, Destination:=Feuil1.Range("A1"))
            .FieldNames = True
            ' etc
    auquel on pourra ajouter
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Private Sub SupprimerNoms()
    Dim i As Long
        With ThisWorkbook
            For i = .Names.Count To 1 Step -1
                If InStr(.Names(i).Name, "DonnéesExternes_") > 0 Then .Names(i).Delete
            Next i
        End With
    End Sub
    pour supprimer les plages nommées DonnéesExternes_XX

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Transformer fichier txt sans ma macro excel
    Par plm222 dans le forum Excel
    Réponses: 0
    Dernier message: 02/02/2013, 18h11
  2. Réponses: 7
    Dernier message: 25/08/2011, 11h03
  3. Réponses: 2
    Dernier message: 14/03/2011, 09h33
  4. Réponses: 2
    Dernier message: 11/03/2011, 18h06
  5. Ecrire dans un fichier txt sans espace avant la 1ère valeur
    Par fatma hamdi dans le forum Fortran
    Réponses: 4
    Dernier message: 17/04/2008, 13h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo