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
| Sub subCompacte()
Dim sh As Excel.Worksheet, rng As Range, tabVil() As Variant
Dim lFin As Long, x As Long, y As Long, z As Long
Const iVil1 As Integer = 2, iVil2 As Integer = 3, iNombre As Integer = 4, iDeb As Integer = 3
Set sh = Application.ThisWorkbook.Worksheets(1)
lFin = sh.Cells(Application.Rows.Count, iVil1).End(xlUp).Row
Set rng = sh.Range(sh.Cells(iDeb, iVil1), sh.Cells(lFin + 1, iNombre))
tabVil() = rng.Value
rng.ClearContents
For x = UBound(tabVil, 1) To LBound(tabVil, 1) Step -1
For y = LBound(tabVil, 1) To x - 1
If (tabVil(y, 1) = tabVil(x, 2)) And (tabVil(y, 2) = tabVil(x, 1)) Then Exit For
Next y
If y < x Then
tabVil(y, 3) = tabVil(y, 3) + tabVil(x, 3)
For z = x + 1 To UBound(tabVil, 1)
tabVil(z - 1, 1) = tabVil(z, 1)
tabVil(z - 1, 2) = tabVil(z, 2)
tabVil(z - 1, 3) = tabVil(z, 3)
If IsNull(tabVil(z, 1)) Then Exit For
Next z
End If
Next x
rng.Value = tabVil()
Set rng = Nothing
Set sh = Nothing
Erase tabVil()
End Sub |
Partager