Export .txt et contenu variable

Bonjour à tous !

J'aimerais pouvoir exporter via un code VBA un fichier .txt.

J'ai le code pour exporter le contenu d'une variable en .txt mais le problème est que mon nombre de ligne a exporter est variable.

Dans le classeur joint, je dois exporter la ligne 1 et 2 mais il se peut qu'il y en ait plus que ça, le tout en gardant le format des cellules et en allant à la ligne pour la ligne suivante...

sub txt()
Dim Chaine As String
Dim Fichier As String
Dim Chemin As String

Chaine = Sheets("fichier txt"). ?????

With ThisWorkbook
    Chemin = .Path & Application.PathSeparator
    Fichier = Chemin & "test"
        With .Sheets("fichier txt")
        Dim f As Integer

    f = FreeFile

    Open Fichier For Output As #f

    Print #f, Chaine

    Close #f

        End With
End With

End Sub

le résultat final doit ressembler à ça

111220191712201950111220190010000110000057501212201900000000000000000013122019

111220191712201950111220190000000000000000001212201900000000000000000013122019

Merci à tous !

13classeur1.xlsx (34.81 Ko)

bonsoir,

une proposition

Sub txt()
    Dim Chaine As String
    Dim Fichier As String
    Dim Chemin As String
    Dim f As Integer

    With ThisWorkbook
        Chemin = .Path & Application.PathSeparator
        Fichier = Chemin & "test.txt"
        With .Sheets("fichier txt")
            dl = .Cells(Rows.Count, 1).End(xlUp).Row

            f = FreeFile

            Open Fichier For Output As #f
            For i = 1 To dl
                Chaine = ""
                For j = 1 To 12

                    Chaine = Chaine & .Cells(i, j).Text
                Next j
                Print #f, Chaine
            Next i
            Close #f

        End With
    End With

End Sub

Cette proposition me convient parfaitement !!

Merci beaucoup h2so4 !

J'ai une autre question du coup mais le problème est résolu !

Est-il possible de faire varier le nombre de colonne ?

Je veux dire que si la ligne 2 est plus courte ou plus longue que 12 colonnes, y a-t-il possibilité de paramétrer ça également ?

Encore merci !

Bonjour,

code modifié pour s'adapter au nombre de colonnes trouvées sur la première ligne.

Sub txt()
    Dim Chaine As String
    Dim Fichier As String
    Dim Chemin As String
    Dim f As Integer

    With ThisWorkbook
        Chemin = .Path & Application.PathSeparator
        Fichier = Chemin & "test.txt"
        With .Sheets("fichier txt")
            dl = .Cells(Rows.Count, 1).End(xlUp).Row
            dc = .Cells(1, Columns.Count).End(xlToLeft).Column
            f = FreeFile

            Open Fichier For Output As #f
            For i = 1 To dl
                Chaine = ""
                For j = 1 To dc
                    Chaine = Chaine & .Cells(i, j).Text
                Next j
                Print #f, Chaine
            Next i
            Close #f

        End With
    End With

End Sub

Merci h2so4 !

J'ai légèrement modifié ton code pour que le compte des colonnes se fasse pour chaque ligne et ça marche.

Voici le code final ! Encore merci !

Sub txt()
    Dim Chaine As String
    Dim Fichier As String
    Dim Chemin As String
    Dim f As Integer

    With ThisWorkbook
        Chemin = .Path & Application.PathSeparator
        Fichier = Chemin & "test.txt"
        With .Sheets("fichier txt")
            dl = .Cells(Rows.Count, 1).End(xlUp).Row
             f = FreeFile

            Open Fichier For Output As #f
            For i = 1 To dl
                Chaine = ""
                dc = Cells(i, Columns.Count).End(xlToLeft).Column
                For j = 1 To dc
                    Chaine = Chaine & .Cells(i, j).Text
                Next j
                Print #f, Chaine
            Next i
            Close #f

        End With
    End With

End Sub
Rechercher des sujets similaires à "export txt contenu variable"