VBA écrire dans fichier texte
Résolu
Mrfrize
Messages postés
188
Date d'inscription
Statut
Membre
Dernière intervention
-
Mrfrize Messages postés 188 Date d'inscription Statut Membre Dernière intervention -
Mrfrize Messages postés 188 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
J'ai un sousi pour écrire dans un fichier texte et je ne vois pas ou est mon problem.
Si quelqu'un vois mon problem ca m'arangerai. Merci d'avance.
Sub Export_to_texte_file()
Dim i As Long
Dim fso, f
Dim a()
i = 4
Do While Sheets("INT").Cells(i, 11).Value <> ""
i = i + 1
Loop
a = Sheets("INT").Range("K4:N" & i - 1).Value
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt", _
ForWriting, True)
f.write (a)
End Sub
J'ai un sousi pour écrire dans un fichier texte et je ne vois pas ou est mon problem.
Si quelqu'un vois mon problem ca m'arangerai. Merci d'avance.
Sub Export_to_texte_file()
Dim i As Long
Dim fso, f
Dim a()
i = 4
Do While Sheets("INT").Cells(i, 11).Value <> ""
i = i + 1
Loop
a = Sheets("INT").Range("K4:N" & i - 1).Value
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt", _
ForWriting, True)
f.write (a)
End Sub
A voir également:
- VBA écrire dans fichier texte
- Fichier bin - Guide
- Fichier epub - Guide
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier .dat - Guide
3 réponses
Bonjour,
Utilise ces constantes :
Cdt
Lupin
Utilise ces constantes :
Const ctePourLecture = 1 Const ctePourEcrire = 2 Const ctePourAjouter = 8 If ( objFSO.FileExists(varNomFic) ) Then Set objFichier = objFSO.OpenTextFile(varNomFic,ctePourAjouter) Else Set objFichier = objFSO.CreateTextFile(varNomFic,ctePourEcrire) End If
Cdt
Lupin
bonjour
dans ton exemple "a</gras" est un tableau. write (a) ne peut pas écrire de tableau.
Il faut alors transformer ce tableau en une variable qui puisse être écrite dans le fichier
Dim i As Long
Dim fso, f
Dim a()
<gras>Dim DernLigne As Long
Dim maligne as String
DernLigne = Range("N" & Rows.Count).End(xlUp).Row
a = Sheets("INT").Range("K4:N" & i - 1).Value
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt", _
ForWriting, True)
For i = LBound(a, 2) To UBound(a, 2)
maligne = maligne & a(1, i)
Next
f.write (maligne)
Bonne suite
dans ton exemple "a</gras" est un tableau. write (a) ne peut pas écrire de tableau.
Il faut alors transformer ce tableau en une variable qui puisse être écrite dans le fichier
Dim i As Long
Dim fso, f
Dim a()
<gras>Dim DernLigne As Long
Dim maligne as String
DernLigne = Range("N" & Rows.Count).End(xlUp).Row
a = Sheets("INT").Range("K4:N" & i - 1).Value
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt", _
ForWriting, True)
For i = LBound(a, 2) To UBound(a, 2)
maligne = maligne & a(1, i)
Next
f.write (maligne)
Bonne suite
J'ai fini par trouver une solution. Voici le code final.
Sub export()
Dim i As Long
Dim fso, f
Dim a, b, c, d
i = 4
While Sheets("INT").Cells(i, 11).Value <> ""
'La valeur de ta cellule
a = Sheets("INT").Cells(i, 11).Value
b = Sheets("INT").Cells(i, 12).Value
c = Sheets("INT").Cells(i, 13).Value
d = Sheets("INT").Cells(i, 14).Value
'Créer le fichier ou ouvre si il existe et le fermé
Open ("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt") For Append As #1
Print #1, a & Chr(9) & b & Chr(9) & c & Chr(9) & d
Close #1
'Tu incrémente i pour copier la valeur de la cellule suivante
i = i + 1
Wend
i = 4
While Sheets("BOOL").Cells(i, 13).Value <> ""
'La valeur de ta cellule
a = Sheets("BOOL").Cells(i, 13).Value
b = Sheets("BOOL").Cells(i, 14).Value
c = Sheets("BOOL").Cells(i, 15).Value
d = Sheets("BOOL").Cells(i, 16).Value
'Créer le fichier ou ouvre si il existe et le fermé
Open ("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt") For Append As #1
Print #1, a & Chr(9) & b & Chr(9) & c & Chr(9) & d
Close #1
'Tu incrémente i pour copier la valeur de la cellule suivante
i = i + 1
Wend
End Sub
Sub export()
Dim i As Long
Dim fso, f
Dim a, b, c, d
i = 4
While Sheets("INT").Cells(i, 11).Value <> ""
'La valeur de ta cellule
a = Sheets("INT").Cells(i, 11).Value
b = Sheets("INT").Cells(i, 12).Value
c = Sheets("INT").Cells(i, 13).Value
d = Sheets("INT").Cells(i, 14).Value
'Créer le fichier ou ouvre si il existe et le fermé
Open ("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt") For Append As #1
Print #1, a & Chr(9) & b & Chr(9) & c & Chr(9) & d
Close #1
'Tu incrémente i pour copier la valeur de la cellule suivante
i = i + 1
Wend
i = 4
While Sheets("BOOL").Cells(i, 13).Value <> ""
'La valeur de ta cellule
a = Sheets("BOOL").Cells(i, 13).Value
b = Sheets("BOOL").Cells(i, 14).Value
c = Sheets("BOOL").Cells(i, 15).Value
d = Sheets("BOOL").Cells(i, 16).Value
'Créer le fichier ou ouvre si il existe et le fermé
Open ("C:\Documents and Settings\Stephane.Dupre\Desktop\TOTO.txt") For Append As #1
Print #1, a & Chr(9) & b & Chr(9) & c & Chr(9) & d
Close #1
'Tu incrémente i pour copier la valeur de la cellule suivante
i = i + 1
Wend
End Sub
Je crois que mon problem viens de la définition de a.
j'ai une erreur en derniere ligne quand je veux écrire dans le fichier.
VB runtime error "13" type mismatch