1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
Option Explicit
Public MaMatrice() As Variant
Sub ExtractionCodeSurPlusieursOnglets()
Dim ShCible As Worksheet
Dim ListeDesOngletsATraiter As Variant
Dim K As Integer
Set ShCible = Sheets("Feuil3")
With ShCible
.Range(.Cells(1, 1), .Cells(.Rows.Count, 2)).ClearContents
End With
ListeDesOngletsATraiter = Array(Sheets("Feuil1"), Sheets("Feuil5"), Sheets("Feuil6"))
For K = 0 To 2
Extraction_Code ListeDesOngletsATraiter(K)
RestituerLaMatrice ShCible
Next K
' Tri de l'onglet cible....
Set ShCible = Nothing
End Sub
Sub Extraction_Code(ByVal FeuilleSource As Worksheet)
Dim MonDico As Object
Dim C As Range
Dim DerniereLigne As Long
Dim ListeCle As Variant
Dim ListeElement As Variant
Dim I As Integer
Dim J As Integer
Dim Tempo1, Tempo2
Set MonDico = CreateObject("Scripting.Dictionary")
With FeuilleSource
DerniereLigne = .Cells(.Rows.Count, 1).End(xlUp).Row ' Colonne à adapter
For Each C In .Range(.Cells(1, 6), .Cells(DerniereLigne, 7))
If Not MonDico.Exists(C.Value) And C.Value <> "" Then MonDico.Add C.Value, C.Value
Next C
ListeCle = MonDico.Keys
ListeElement = MonDico.Items
' Tri par ordre alphabétique dans la variable tableau
'----------------------------------------------------
For I = 0 To MonDico.Count - 2
For J = I + 1 To MonDico.Count - 1
If ListeElement(I) > ListeElement(J) Then
Tempo1 = ListeCle(J)
Tempo2 = ListeElement(J)
ListeElement(J) = ListeElement(I)
ListeCle(J) = ListeCle(I)
ListeCle(I) = Tempo1
ListeElement(I) = Tempo2
End If
Next J
Next I
' Initialisation et remplissage de la matrice
'--------------------------------------------
ReDim MaMatrice(UBound(ListeCle), 1)
For I = LBound(ListeCle, 1) To UBound(ListeCle, 1)
MaMatrice(I, 0) = ListeCle(I)
Next I
' Cumul des produits colonne 1 * colonne 2 pour chaque référence
'---------------------------------------------------------------
For I = LBound(MaMatrice, 1) To UBound(MaMatrice, 1)
For Each C In .Range(.Cells(1, 6), .Cells(DerniereLigne, 6))
If C = MaMatrice(I, 0) Then MaMatrice(I, 1) = MaMatrice(I, 1) + (C.Offset(0, 1 - 6) * C.Offset(0, 2 - 6))
Next C
For Each C In .Range(.Cells(1, 7), .Cells(DerniereLigne, 7))
If C = MaMatrice(I, 0) Then MaMatrice(I, 1) = MaMatrice(I, 1) + (C.Offset(0, 1 - 7) * C.Offset(0, 2 - 7))
Next C
Next I
End With
' Restitution
'------------
' With FeuilleCible ' A adapter
' .Range(.Cells(1, 1), .Cells(.Rows.Count, 2)).ClearContents
' End With
Set MonDico = Nothing
End Sub
Sub RestituerLaMatrice(ByVal FeuilleCible As Worksheet)
Dim DerniereLigneCible As Long, MonItem As Long
Dim AireCible As Range, CelluleCible As Range
Dim ReferenceTrouvee As Boolean
With FeuilleCible
DerniereLigneCible = .Cells(.Rows.Count, 1).End(xlUp).Row
Set AireCible = .Range(.Cells(1, 1), .Cells(DerniereLigneCible, 1))
For MonItem = LBound(MaMatrice, 1) To UBound(MaMatrice, 1)
ReferenceTrouvee = False
For Each CelluleCible In AireCible
If CelluleCible = MaMatrice(MonItem, 0) Then
CelluleCible.Offset(0, 1) = CelluleCible.Offset(0, 1) + MaMatrice(MonItem, 1)
ReferenceTrouvee = True
End If
Next CelluleCible
If ReferenceTrouvee = False Then
.Cells(DerniereLigneCible + 1, 1) = MaMatrice(MonItem, 0)
.Cells(DerniereLigneCible + 1, 2) = MaMatrice(MonItem, 1)
DerniereLigneCible = DerniereLigneCible + 1
End If
Next MonItem
Set AireCible = Nothing
End With
End Sub |
Partager