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
|
'A jouter la référence à microsoft Scripting Runtiome
Option Base 1
Sub FusionFeuilles2èmeEssai()
Dim sh3 As Worksheet
Dim mDico As New Dictionary
Dim feuille As Variant
Dim F As Integer
Dim i As Integer
'on redimentionne le tableau par raport au nombre de ligne utilisée dans le sheets 1
ReDim tablofinal(Sheets(1).[a65536].End(xlUp).Row, 2)
feuille = Array("feuil1", "feuil2")
Set sh3 = ThisWorkbook.Sheets("Feuil3")
'on place les entetes dans la variable tablofinal
tablofinal(1, 1) = Sheets(1).Cells(1, 1).Text: tablofinal(1, 2) = Sheets(1).Cells(1, 2).Value
For F = 1 To UBound(feuille)
'Lecture feuille
With Sheets(feuille(F))
i = 2 'Ligne 1 = Titre
While .Cells(i, 1) <> ""
If Not mDico.Exists(.Cells(i, 1).Text) Then
'Cas ou n'existe pas créé l'enregistrement
mDico.Add .Cells(i, 1).Text, .Cells(i, 2).Value
tablofinal(i, 1) = .Cells(i, 1).Text
tablofinal(i, 2) = .Cells(i, 2).Value
Else
For a = 1 To UBound(tablofinal) - 1
If tablofinal(a, 1) = .Cells(i, 1).Text Then tablofinal(a, 2) = tablofinal(a, 2) + .Cells(i, 2).Text
Next
End If
i = i + 1 'Ligne suivante
Wend
End With
Next
sh3.Cells(1, 1).Resize(UBound(tablofinal), 2) = tablofinal
End Sub |