
Option Explicit
Sub Aggiorna_lista()
Dim LastRow As Long
Const Voce As String = "B"
Const Num As String = "C"
Const StartRow As Long = 5
LastRow = Cells(Rows.Count, Voce).End(3).Row
If StartRow > LastRow Then Exit Sub
Dim I As Long
Dim K As Long
Dim Somma As Long
Dim Articolo As String
With ActiveSheet.ListBox1
.ListFillRange = ""
.Clear
.ColumnCount = 2
.ColumnWidths = "120;12"
.Width = 180
For I = StartRow To LastRow
Articolo = Cells(I, Voce)
.AddItem Articolo
Somma = Cells(I, Num)
For K = I + 1 To LastRow
If Articolo = Cells(K, Voce) Then
I = K
Somma = Somma + Cells(K, Num)
Else
Exit For
End If
Next
.List(.ListCount - 1, 1) = Somma
Next I
End With
End Sub
|
'In un modulo
Option Explicit
Sub Aggiorna_lista()
Dim LastRow As Long
Const Voce As String = "B"
Const Num As String = "C"
Const startRow As Long = 5
LastRow = Cells(Rows.Count, Voce).End(3).Row
If startRow > LastRow Then Exit Sub
Dim Data As Variant, Lista(), arr()
'Original List
Data = Range(Cells(startRow, Voce), Cells(LastRow, Num))
'Utilizzo un array per filtrare le voci consecutive presenti in data
ReDim Lista(1 To UBound(Data, 1), 1 To 2)
Dim I As Long
Dim K As Long
Dim Riga As Long
With ActiveSheet.ListBox1
.ListFillRange = ""
.Clear
.ColumnCount = 2
.ColumnWidths = "120;12"
.Width = 180
For I = 1 To UBound(Data, 1)
Riga = Riga + 1
Lista(Riga, 1) = Data(I, 1)
Lista(Riga, 2) = Data(I, 2)
For K = I + 1 To UBound(Data, 1)
If Data(I, 1) = Data(K, 1) Then
I = K
Lista(Riga, 2) = Lista(Riga, 2) + Data(I, 2)
Else
Exit For
End If
Next
Next I
'Utilizzo un array per ridimensionare le voci presenti in lista
ReDim arr(1 To Riga, 1 To 2)
For I = 1 To Riga
arr(I, 1) = Lista(I, 1)
arr(I, 2) = Lista(I, 2)
Next
.List = arr
End With
End Sub
|
