Bonjour à tous,
Voici deux bouts de code pour insérer un tableau 1D ou 2D sous forme de matrice dans un fichier texte de la manière la plus simple possible. À noter que ces codes créer le fichier texte.
Tableau 1D :
Sub Insert_tbl1D()
Dim tbl(1 To 5) As String
tbl(1) = "Un"
tbl(2) = "Deux"
tbl(3) = "Trois"
tbl(4) = "Quatre"
tbl(5) = "Cinq"
Chemin = "C:\Users\" & Environ("USERNAME") & "\Desktop\NomFichierTXT"
'Définition du chemin du fichier
FichierTxt = Chemin & ".txt"
'Ajout de l'array dans le fichier texte
Open FichierTxt For Binary As #1
Put #1, , Join(tbl, vbCrLf)
Close #1
End Sub
Tableau 2D :
Sub Insert_tbl2D()
Dim tbl(1 To 3, 1 To 2) As String
Dim ligne As String
tbl(1, 1) = "Un"
tbl(1, 2) = "Deux"
tbl(2, 1) = "Trois"
tbl(2, 2) = "Quatre"
tbl(3, 1) = "Cinq"
tbl(3, 2) = "Six"
Chemin = "C:\Users\" & Environ("USERNAME") & "\Desktop\NomFichierTXT"
'Définition du chemin du fichier
FichierTxt = Chemin & ".txt"
ligne = ""
For i = LBound(tbl, 1) To UBound(tbl, 1)
For j = LBound(tbl, 2) To UBound(tbl, 2)
ligne = ligne & tbl(i, j) & vbTab
Next j
ligne = ligne & vbCrLf
Next i
'Ajout de l'array dans le fichier texte
Open FichierTxt For Binary As #1
Put #1, , ligne
Close #1
End Sub
Bonne journée !
Baboutz