Ajouter des petits enfants à un treeview en VBA

lachapellejf2 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention   -  
Kalissi Messages postés 218 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

J'utilise le code suivant pour remplir mon treeview. J'essai de trouver la formule pour ajouter des grandchild au childnodes, mais sans succès.
Les données pour les grandchild sont structuré de la même manière que pour les childnodes mais dans la sheet2.

Quelqu'un à une solution à me proposer?
Merci d'avance!


Private Sub UserForm_Initialize()
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 1).Value, Text:=Sheet1.Cells(1, 1).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 2).Value, Text:=Sheet1.Cells(1, 2).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 3).Value, Text:=Sheet1.Cells(1, 3).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 4).Value, Text:=Sheet1.Cells(1, 4).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 5).Value, Text:=Sheet1.Cells(1, 5).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 6).Value, Text:=Sheet1.Cells(1, 6).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 7).Value, Text:=Sheet1.Cells(1, 7).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 8).Value, Text:=Sheet1.Cells(1, 8).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 9).Value, Text:=Sheet1.Cells(1, 9).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 10).Value, Text:=Sheet1.Cells(1, 10).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 11).Value, Text:=Sheet1.Cells(1, 11).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 12).Value, Text:=Sheet1.Cells(1, 12).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 13).Value, Text:=Sheet1.Cells(1, 13).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 14).Value, Text:=Sheet1.Cells(1, 14).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 15).Value, Text:=Sheet1.Cells(1, 15).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 16).Value, Text:=Sheet1.Cells(1, 16).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 17).Value, Text:=Sheet1.Cells(1, 17).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 18).Value, Text:=Sheet1.Cells(1, 18).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 19).Value, Text:=Sheet1.Cells(1, 19).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 20).Value, Text:=Sheet1.Cells(1, 20).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 21).Value, Text:=Sheet1.Cells(1, 21).Value

Call FillChildNodes(1, Sheet1.Cells(1, 1).Value)
Call FillChildNodes(2, Sheet1.Cells(1, 2).Value)
Call FillChildNodes(3, Sheet1.Cells(1, 3).Value)
Call FillChildNodes(4, Sheet1.Cells(1, 4).Value)
Call FillChildNodes(5, Sheet1.Cells(1, 5).Value)
Call FillChildNodes(6, Sheet1.Cells(1, 6).Value)
Call FillChildNodes(7, Sheet1.Cells(1, 7).Value)
Call FillChildNodes(8, Sheet1.Cells(1, 8).Value)
Call FillChildNodes(9, Sheet1.Cells(1, 9).Value)
Call FillChildNodes(10, Sheet1.Cells(1, 10).Value)
Call FillChildNodes(11, Sheet1.Cells(1, 11).Value)
Call FillChildNodes(12, Sheet1.Cells(1, 12).Value)
Call FillChildNodes(13, Sheet1.Cells(1, 13).Value)
Call FillChildNodes(14, Sheet1.Cells(1, 14).Value)
Call FillChildNodes(15, Sheet1.Cells(1, 15).Value)
Call FillChildNodes(16, Sheet1.Cells(1, 16).Value)
Call FillChildNodes(17, Sheet1.Cells(1, 17).Value)
Call FillChildNodes(18, Sheet1.Cells(1, 18).Value)
Call FillChildNodes(19, Sheet1.Cells(1, 19).Value)
Call FillChildNodes(20, Sheet1.Cells(1, 20).Value)
Call FillChildNodes(21, Sheet1.Cells(1, 21).Value)




End Sub


Sub FillChildNodes(ByVal col As Integer, ByVal continent As String)
Dim LastRow As Long
With Sheet1
    LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With

Dim counter As Integer
counter = 1
Dim country As Range

For Each country In Range(Sheet1.Cells(2, col), Sheet1.Cells(LastRow, col))
Debug.Print ("country:" & country)
TreeView1.Nodes.Add Sheet1.Cells(1, col).Value, tvwChild, continent + CStr(counter), country
counter = counter + 1
Next country

End Sub





A voir également:

3 réponses

Kalissi Messages postés 218 Date d'inscription   Statut Membre Dernière intervention   20
 
0
lachapellejf2 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention  
 
Bonjour Kalissi, j'avais déja trouvé ce site pour m'aider, malgré ca, je n'arrive pas à trouver le bon code pour ajouter un 3e niveau. Ajouter un petit enfant à un enfant d'un parent.
0
Kalissi Messages postés 218 Date d'inscription   Statut Membre Dernière intervention   20
 
Bonjour,

En VB.NET (VS2010/2015), j'ajoute des nœuds en créant d'abord le nœud enfant.
J'ai un treeview à multiples étages (~ 15 étages).

Exemple non testé en VBA :

Public Sub Test()

    Dim NoeudParent As Nodes
    Dim NoeudEnfant As Nodes
    Dim NoeudPetitEnfants As Nodes

    NoeudPetitEnfants.Add Key:=Sheets("Feuil1").Cells(1, col).Value, Text:=Continent + CStr(counter)
    NoeudEnfants.Add NoeudPetitEnfants
    NoeudParents.Add NoeudEnfants

End Sub


Bien sur, je créé une méthode qui génère un arbre généalogique par récursivité. Donc je n'ai pas
à gérer les sous nœuds des enfants de primier niveau...
0
Kalissi Messages postés 218 Date d'inscription   Statut Membre Dernière intervention   20
 
Bonjour,

Voici la solution :-) À adapter ... selon votre code ...

L'astuce est de nommé le nœud enfant existant ...

Ici le nœud enfant est [ Key:="Enf" + CStr(counter) ], codé en dur [ "Enf1" ] pour la démo.

' Création du noeud Petit Enfant
FrmUtilisateur.TreeView1.Nodes.Add "Enf1", tvwChild, Key:="PetEnf" + CStr(counter), Text:="Paul"


Sub FillChildNodes(ByVal col As Integer, ByVal Continent As String)

    Dim LastRow As Long

    With Sheets("Feuil1")
        LastRow = .Cells(.Rows.Count, col).End(xlUp).Row
    End With

    Dim counter As Integer
    counter = 1
    Dim country As Range

    For Each country In Range(Sheets("Feuil1").Cells(2, col), Sheets("Feuil1").Cells(LastRow, col))
        Debug.Print ("country:" & country)
        ' Création de noeud Parent
        FrmUtilisateur.TreeView1.Nodes.Add Key:=Sheets("Feuil1").Cells(1, col).Value, Text:=Continent + CStr(counter)
        ' Création du noeud Enfant
        FrmUtilisateur.TreeView1.Nodes.Add Sheets("Feuil1").Cells(1, col).Value, tvwChild, Key:="Enf" + CStr(counter), Text:="Claude"
        ' Création du noeud Petit Enfant
        FrmUtilisateur.TreeView1.Nodes.Add "Enf1", tvwChild, Key:="PetEnf" + CStr(counter), Text:="Paul"
        counter = counter + 1
        col = col + 1
    Next country

End Sub
'


K
0