VBA: Excel utilise un .txt pour se remplir
Bonjour à tous!
J'utilise VBA pour mes projets d'étude et j'ai besoin d'aide pour qu'un excel utilise un .txt pour se remplir!
Mon fichier texte ce présente comme ceci:
"
module NomModule1
// DescriptionNomTypeALigne1
// DescriptionNomTypeALigne2
Type1 NomTypeA
{
ParamA1,
ParamA2,
ParamA3
};
// DescriptionNomTypeBLigne1
// DescriptionNomTypeBLigne2
Type2 NomTypeB
{
TypeZ ParamB1,
TypeY ParamB2,
TypeZ ParamB3,
TypeZ ParamB4
};
#SpecificiteNomTypeB
module NomModule2
// DescriptionNomTypeCLigne1
// DescriptionNomTypeCLigne2
Type1 NomTypeC
{
ParamC1,
ParamC2,
ParamC3
};
#SpecificiteNomTypeC
// DescriptionNomTypeDLigne1
// DescriptionNomTypeDLigne2
Type2 NomTypeD
{
TypeZ ParamD1,
TypeY ParamD2,
TypeZ ParamD3,
TypeZ ParamD4
};
"
J'aimerai que l'importation permet de faire ceci: (les ; permet de séparer les colonnes)
NomModule1 ; Type1; NomTypeA ; ParamA1 ; ; DescriptionNomTypeALigne1 & DescriptionNomTypeALigne2;
NomModule1 ; Type1; NomTypeA ; ParamA2 ; ; DescriptionNomTypeALigne1 & DescriptionNomTypeALigne2;
NomModule1 ; Type1; NomTypeA ; ParamA3 ; ; DescriptionNomTypeALigne1 & DescriptionNomTypeALigne2;
NomModule1 ; Type2; NomTypeB ; ParamB1 ; TypeZ ; DescriptionNomTypeBLigne1 & DescriptionNomTypeBLigne2; SpecificiteNomTypeB
NomModule1 ; Type2; NomTypeB ; ParamB2 ; TypeY ; DescriptionNomTypeBLigne1 & DescriptionNomTypeBLigne2; SpecificiteNomTypeB
NomModule1 ; Type2; NomTypeB ; ParamB3 ; TypeZ ; DescriptionNomTypeBLigne1 & DescriptionNomTypeBLigne2; SpecificiteNomTypeB
NomModule1 ; Type2; NomTypeB ; ParamB4 ; TypeZ ; DescriptionNomTypeBLigne1 & DescriptionNomTypeBLigne2; SpecificiteNomTypeB
NomModule2 ; Type1; NomTypeC ; ParamC1 ; ; DescriptionNomTypeCLigne1 & DescriptionNomTypeCLigne2; SpecificiteNomTypeC
NomModule2 ; Type1; NomTypeC ; ParamC2 ; ; DescriptionNomTypeCLigne1 & DescriptionNomTypeCLigne2; SpecificiteNomTypeC
NomModule2 ; Type1; NomTypeC ; ParamC3 ; ; DescriptionNomTypeCLigne1 & DescriptionNomTypeCLigne2; SpecificiteNomTypeC
NomModule2 ; Type2; NomTypeD ; ParamD1 ; TypeZ ; DescriptionNomTypeDLigne1 & DescriptionNomTypeDLigne2;
NomModule2 ; Type2; NomTypeD ; ParamD2 ; TypeY ; DescriptionNomTypeDLigne1 & DescriptionNomTypeDLigne2;
NomModule2 ; Type2; NomTypeD ; ParamD3 ; TypeZ ; DescriptionNomTypeDLigne1 & DescriptionNomTypeDLigne2;
NomModule2 ; Type2; NomTypeD ; ParamD4 ; TypeZ ; DescriptionNomTypeDLigne1 & DescriptionNomTypeDLigne2;
Mon souci dans ce projet est :
- Ouvrir le fichier texte
'Ouverture .Txt
Ce bout de code me permet d'ouvrir un .xls, que dois je faire pour qu'il ouvre un .txt!
Dossier = "d:\Projet\Essai\"
NomFichier = "Test.txt"
OpenText Filename:=(Dossier & NomFichier), Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False
- Récupérer les données du fichier texte.
Je ne sais pas comment commencer au début du fichier .Txt et récolté les données.
A partir du moment où je sais comment aller chercher chaque donnée, le mettre en forme est facile.
Pourriez vous m'aider à comprendre comment exploiter un .txt!
Merci d'avance!
Bonjour,
Un début de piste :
Sub Test()
Dim Tbl() As String
Dim I As Integer
Dim J As Integer
Dim K As Integer
Tbl = Lire("D:\Projet\Essai\Test.txt")
For I = 1 To UBound(Tbl, 2)
'si la dimension 1 du tableau n'est pas vide
'(il s'y trouve le nom du module) inscrit en
'colonne A
If Tbl(1, I) <> "" Then
K = 1
J = J + 1
Cells(J, K) = Tbl(1, I)
'sinon, inscrit dans les cellules de la même ligne
'que le nom du module
Else
K = K + 1
Cells(J, K) = Tbl(2, I)
End If
Next I
Erase Tbl
End Sub
Function Lire(Fichier As String) As String()
Dim TblRecup() As String
Dim Ligne As String
Dim I As Long
Open Fichier For Input As #1
Do While Not EOF(1)
Line Input #1, Ligne
'supprime le guillemet
Ligne = Replace(Ligne, """", "")
'ne récupère que les lignes non vide
If Ligne <> "" Then
I = I + 1
ReDim Preserve TblRecup(1 To 2, 1 To I)
'si dans la ligne se trouve le mot "Module"
If InStr(Ligne, "Module") <> 0 Then
'en récupère le nom
TblRecup(1, I) = Trim(Right(Ligne, Len(Ligne) - 7))
Else
'puis ensuite, récupère toutes les lignes qui en dépendent
TblRecup(2, I) = Trim(Ligne)
End If
End If
Loop
Close #1
Lire = TblRecup
End FunctionHervé.
Hervé,
Tout d'abord merci pour ton aide.
avec Do While Not EOF(1)
Line Input #1, Ligne
Loop
J'ai compris comment avancé ligne par ligne.
J'ai repris ton code est ajouté quelques modifications pour s'ajuster à mon besoin:
Function Lire(Fichier As String) As String()
Dim TblRecup() As String
Dim TabParam() As String, IndexParam As Long
Erase TabParam()
Erase TblRecup()
Dim Ligne As String
Dim ColonneTableau As Long
Dim ColonneEnCours As Long
Dim NumeroLigne As Long
Dim ModuleEnCours As String, TypeMessage As String, NomType As String, Param As String, TypeParam As String, Description As String, Specificite As String
' on initialise à la première ligne
NumeroLigne = 0
ColonneEnCours = 0
ColonneTableau = 7
Open Fichier For Input As #1
Do While Not EOF(1)
Line Input #1, Ligne
'supprime le guillemet
'Ligne = Replace(Ligne, """", "") 'les guillemet délimité le texte dans mon poste.
'ne récupère que les lignes non vide
If Trim(Ligne) <> "" Then
'si dans la ligne se trouve le mot "Module"
If InStr(LCase(Ligne), "module") <> 0 Then
ModuleEnCours = Trim(Right(Ligne, Len(Ligne) - 7)) 'récupère le nom du module
'si on trouve une structure de message
ElseIf InStr(LCase(Ligne), "type1") <> 0 Or InStr(LCase(Ligne), "type2") <> 0 Or InStr(LCase(Ligne), "type3") <> 0 Then 'On énumère tous les types de message possible
Ligne = Trim(Ligne)
TypeMessage = Left(Ligne, InStr(Ligne, " ") - 1)
NomType = Right(Ligne, Len(Ligne) - InStr(Ligne, " "))
IndexParam = 0
'Description = 'Concaténé les lignes commençant par // avant la ligne
Do While Not EOF(1)
Line Input #1, Ligne
If InStr(Ligne, "{") = 0 And InStr(Ligne, "}") = 0 Then
IndexParam = IndexParam + 1
ReDim Preserve TabParam(1 To 2, 1 To IndexParam)
If InStr(Trim(Ligne), " ") <> 0 Then 'si la forme du paramètre est Type Param
TabParam(2, IndexParam) = Left(Trim(Ligne), InStr(Trim(Ligne), " ") - 1) 'TypeParam
TabParam(1, IndexParam) = Replace(Right(Trim(Ligne), Len(Trim(Ligne)) - InStr(Trim(Ligne), " ")), ",", "") 'Param (on supprime la virgule si existant)
Else 'si la forme du paramètre est Param
TabParam(2, IndexParam) = ""
TabParam(1, IndexParam) = Replace(Trim(Ligne), ",", "") 'Param (on supprime la virgule si existant)
End If
ElseIf Trim(Ligne) = "" Then 'La structure de message est fini
Exit Do
ElseIf InStr(Ligne, "}") <> 0 Then 'On arrive à la fin de la structure et on cherche s'il y a une specificité
Line Input #1, Ligne
If Trim(Ligne) = "" Then
Exit Do
Else
Specificite = Trim(Ligne)
End If
End If
Loop
'On renseigne les cases du tableau avec ce que les données
For I = 1 To UBound(TabParam, 2)
NumeroLigne = NumeroLigne + 1
ReDim Preserve TblRecup(1 To NumeroLigne, 1 To ColonneTableau) 'Lorsque NumeroLigne = 2 alors problème d'indice
TblRecup(NumeroLigne, 1) = NomModule
TblRecup(NumeroLigne, 2) = TypeMessage
TblRecup(NumeroLigne, 3) = NomType
TblRecup(NumeroLigne, 4) = TabParam(1, I)
TblRecup(NumeroLigne, 5) = TabParam(2, I)
'TblRecup(NumeroLigne, 6) = Description
TblRecup(NumeroLigne, 7) = Specificite
Next
Erase TabParam()
End If
End If
Loop
Close #1
Lire = TblRecup
Erase TabParam()
Erase TblRecup()
End FunctionCependant j'ai deux soucis:
1) Pourquoi
ReDim Preserve TblRecup(1 To NumeroLigne, 1 To ColonneTableau) 'Lorsque NumeroLigne = 2 alors problème d'indicefait échouer le processus lorsque NumeroLigne = 2?
2) Lorsque j'ai trouvé la structure ( InStr(LCase(Ligne), "type1") <> 0 Or InStr(LCase(Ligne), "type2") <> 0 .....
Comment puis je remonter dans le texte pour récupérer chaque ligne de description (commençant par //) jusqu'à une ligne vide?
Merci d'avance!
Bonjour,
1) Pourquoi
ReDim Preserve TblRecup(1 To NumeroLigne, 1 To ColonneTableau) 'Lorsque NumeroLigne = 2 alors problème d'indicefait échouer le processus lorsque NumeroLigne = 2?
Tout simplement parce qu'il n'est pas possible, dans un tableau, de redimensionner dynamiquement la première dimension, seule la dernière est redimensionnable.
2) Lorsque j'ai trouvé la structure ( InStr(LCase(Ligne), "type1") <> 0 Or InStr(LCase(Ligne), "type2") <> 0 .....
Comment puis je remonter dans le texte pour récupérer chaque ligne de description (commençant par //) jusqu'à une ligne vide?
Je vais y jeter un oeil !
Hervé.
Bonjour,
J'ai finalement réussi à naviguer dans le texte.
Une méthode brutale mais efficace est de faire un tableau dynamique avec 1 ligne = 1 case.
J'ai trouver la méthode sur http://www.pise.info/vb/partie8.htm
Avec la partie de code suivant:
Dim T() as string
...
Open "C:\Monfichier.txt" As #1 For Input
i = -1
While Not Eof(1)
i = i + 1
Redim Preserve T(i)
Line Input #1, T(i)
WendFinalement voici le code qui permet de répondre à mon besoin.
Option Explicit
Sub Test()
Dim Tbl() As String
Dim NumeroLigne As Long
Tbl = Lire2("D:\Projet\Essai\Test.txt")
For NumeroLigne = 1 To UBound(Tbl, 2)
Cells(NumeroLigne, 1) = Tbl(1, NumeroLigne)
Cells(NumeroLigne, 2) = Tbl(2, NumeroLigne)
Cells(NumeroLigne, 3) = Tbl(3, NumeroLigne)
Cells(NumeroLigne, 4) = Tbl(4, NumeroLigne)
Cells(NumeroLigne, 5) = Tbl(5, NumeroLigne)
Cells(NumeroLigne, 6) = Tbl(6, NumeroLigne)
Cells(NumeroLigne, 7) = Tbl(7, NumeroLigne)
Next
Erase Tbl
End Sub
'________________________________________________
Function Lire2(Fichier As String) As String()
Dim TblRecup() As String
Dim TabParam() As String, IndexParam As Long
Erase TabParam()
Erase TblRecup()
Dim Ligne As String
Dim ColonneTableau As Long
Dim NumeroLigne As Long
Dim ModuleEnCours As String, TypeMessage As String, NomType As String, Param As String, TypeParam As String, Description As String, Specificite As String
' on initialise à la première ligne
NumeroLigne = 0
ColonneTableau = 7
Open Fichier For Input As #1
'Mettre le texte dans un tableau dynamique, chaque ligne est une case.
Dim TblStockTXT() As String, Index As Long, IndexDescrip As Long
Index = -1
While Not EOF(1)
Index = Index + 1
ReDim Preserve TblStockTXT(Index)
Line Input #1, TblStockTXT(Index)
Wend
Index = -1
Do While Index < UBound(TblStockTXT)
Index = Index + 1
Ligne = TblStockTXT(Index)
'supprime le guillemet
'Ligne = Replace(Ligne, """", "") 'les guillemet délimité le texte dans mon poste.
'ne récupère que les lignes non vide
If Trim(Ligne) <> "" Then
'si dans la ligne se trouve le mot "Module"
If InStr(LCase(Ligne), "module") <> 0 Then
ModuleEnCours = Trim(Right(Ligne, Len(Ligne) - 7)) 'récupère le nom du module
'si on trouve une structure de message
ElseIf InStr(LCase(Ligne), "type1") <> 0 Or InStr(LCase(Ligne), "type2") <> 0 Or InStr(LCase(Ligne), "type3") <> 0 Then 'On énumère tous les types de message possible
Ligne = Trim(Ligne)
TypeMessage = Trim(Left(Ligne, InStr(Ligne, " ") - 1))
NomType = Trim(Right(Ligne, Len(Ligne) - InStr(Ligne, " ")))
IndexParam = 0
'Description = 'Concaténé les lignes commençant par // avant la ligne
Description = ""
IndexDescrip = Index
Do While IndexDescrip > 0 And Ligne <> ""
IndexDescrip = IndexDescrip - 1
Ligne = Trim(TblStockTXT(IndexDescrip))
If InStr(Ligne, "//") <> 0 Then
Description = Trim(Right(Ligne, Len(Ligne) - 3) & " " & Description)
Else
Exit Do
End If
Loop
Do While Index < UBound(TblStockTXT)
Index = Index + 1
Ligne = TblStockTXT(Index)
If Trim(Ligne) = "" Then 'La structure de message est fini
Exit Do
ElseIf InStr(Ligne, "{") = 0 And InStr(Ligne, "}") = 0 Then
IndexParam = IndexParam + 1
ReDim Preserve TabParam(1 To 2, 1 To IndexParam)
If InStr(Trim(Ligne), " ") <> 0 Then 'si la forme du paramètre est Type Param
TabParam(2, IndexParam) = Left(Trim(Ligne), InStr(Trim(Ligne), " ") - 1) 'TypeParam
TabParam(1, IndexParam) = Replace(Right(Trim(Ligne), Len(Trim(Ligne)) - InStr(Trim(Ligne), " ")), ",", "") 'Param (on supprime la virgule si existant)
Else 'si la forme du paramètre est Param
TabParam(2, IndexParam) = ""
TabParam(1, IndexParam) = Replace(Trim(Ligne), ",", "") 'Param (on supprime la virgule si existant)
End If
ElseIf InStr(Ligne, "}") <> 0 Then 'On arrive à la fin de la structure et on cherche s'il y a une specificité
If Index < UBound(TblStockTXT) Then
Index = Index + 1
Ligne = TblStockTXT(Index)
If Trim(Ligne) = "" Then
Exit Do
Else
Specificite = Trim(Ligne)
End If
End If
End If
If Ligne = "" Then
Exit Do
End If
Loop
'On renseigne les cases du tableau avec ce que les données
For IndexDescrip = 1 To UBound(TabParam, 2)
NumeroLigne = NumeroLigne + 1
ReDim Preserve TblRecup(1 To ColonneTableau, 1 To NumeroLigne) 'Lorsque NumeroLigne = 2 alors problème d'indice
TblRecup(1, NumeroLigne) = ModuleEnCours
TblRecup(2, NumeroLigne) = TypeMessage
TblRecup(3, NumeroLigne) = NomType
TblRecup(4, NumeroLigne) = TabParam(1, IndexDescrip)
TblRecup(5, NumeroLigne) = TabParam(2, IndexDescrip)
TblRecup(6, NumeroLigne) = Description
TblRecup(7, NumeroLigne) = Specificite
Next
Specificite = ""
Description = ""
NomType = ""
TypeMessage = ""
Erase TabParam()
End If
End If
Loop
Close #1
Lire2 = TblRecup
Erase TabParam
Erase TblRecup
Erase TblStockTXT
End FunctionMerci Hervé pour ton aide.
Je suis preneur d'une méthode Line Input #1, Ligne inversé (qui remonte au lieu de descendre)