Dico à 2 dimensions
Hello à tous,
J'ai un fichier excel avec:
colonne 1: une catégorie (book)
colonne 2: un numero unique (ISIN)
colonne 3 à X des données
Je souhaite réaliser un dictionnaire à deux dimensions, autrement dit: un dictionnaire de dictionnaire et j'ai un soucis de synthaxe.
Le dictionnaire DicBook associe à la clef "BOOK" un dictionnaire "DicInstr", et le dictionnaire "DicInstr" associe à "ISIN" un module de classe "ClsInstr".
J'ai déjà réalisé une structure similaire pour mon ancien employeur, mais je n'arrive plus à retrouver la bonne synthaxe ... Si quelqu'un est à même de l'aider je suis preneur.
Un bout de code des souvenirs que j'avais du truc, ce n'est ni parfait ni complet malheuresment:
Sub Dictionnaire()
Dim Wk As Workbook
Dim sh_Instr As Worksheet
Dim Book As Variant
Dim Cfin As Variant
Dim i As Long
Dim Caract As New clsInstr
Dim DicBook As New Dictionary
Dim DicInstr As New Dictionary
Set Wk = ActiveWorkbook
Set sh_Instr = Wk.sheets(1)
'0) Activier microsoft runtime
'1) Trier par big book
sh_Instr.Range("A:C").Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
'2) Créer/remplir les dictionnaires
i = 2
Book = CLng(sh_instr.Cells(i, 1).Value)
Cfin = CLng(sh_instr.Cells(i, 2).Value)
Set DicBook = New Dictionary
If Not DicBook.exists(Book) Then
DicBook.Add Book, DicInstr 'Si le book n'est pas encore existant dans le DicBook, faire une association'
If Not DicInstr.exits(Cfin) Then 'Idem si le Cfin n'est pas dans DicInstr, faire une association'
Set Caract = New clsInstr
DicInstr.Add Cfin, Caract
Else
End If
Else
If Not DicInstr.exits(Cfin) Then
Set clsInstr = DicInstr(Cfin)
DicInstr.Add Cfin, clsInstr
Else
End If
End If
End Sub
Un grand merci
- Messages
- 4'088
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Bonjour,
Je verrais un code de ce type
Sub Dictionnaire()
'// ajouter référence Microsoft Scripting Runtime
Dim Wk As Workbook
Dim sh_Instr As Worksheet
Dim Book As Variant
Dim Cfin As Variant
Dim i As Long
Dim DicBook As New Dictionary
Dim DicInstr As Dictionary
Set Wk = ActiveWorkbook
Set sh_Instr = Wk.Sheets(1)
'1) Trier par big book
sh_Instr.Range("A:C").Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes
'2) Créer/remplir les dictionnaires
i = 2
Book = CLng(sh_Instr.Cells(i, "A").Value)
Cfin = CLng(sh_Instr.Cells(i, "B").Value)
If Not DicBook.exists(Book) Then DicBook.Add Book, New Dictionary 'Si le book n'est pas encore existant dans le DicBook, faire une association'
Set DicInstr = DicBook(Book)
If Not DicInstr.exists(Cfin) Then DicInstr.Add Cfin, New clsInstr 'Idem si le Cfin n'est pas dans DicInstr, faire une association'
Set clsInstr = DicInstr(Cfin)
End Sub
Hello Thev,
En faisant ce que tu proposes on a deux dictionnaire distincts et non imbriqué l'un dans l'autre. Après une grosse nuit à gratter j'ai retrouvé ce que je cherchais, je vais le psoter si jamais ça peut aider.
Les lignes en gras sont celles à changer en fonction des élements passés en module de classe typé comme cela:
Public name as string
i = 2
While i < 125
Book = CLng(sh_Instr.Cells(i, 1).Value)
Cfin = CLng(sh_Instr.Cells(i, 2).Value)
If DicBook.Exists(Book) Then
Set DicInstr = DicBook(Book)
If DicInstr.Exists(Cfin) Then
Set CurInstr = DicInstr(Cfin)
[b]CurInstr.Name = CStr(Cells)[/b]
Else
Set CurInstr = New clsInstr
[b]CurInstr.Name = CStr(Cells)[/b]
Call DicInstr.Add(Cfin, CurInstr)
End If
Else
Set DicInstr = New Dictionary
Set CurInstr = New clsInstr
[b]CurInstr.Name = CStr(Cells)[/b]
Call DicInstr.Add(Cfin, CurInstr)
Call DicBook.Add(Book, DicInstr)
End If
i = i + 1
Wend
Ce bout de code n'est pas parfait mais c'est plus dans l'esprit de ce que je cherchais