Option Explicit
'Definizione delle variabili usate nella macro
Dim I As Long, J As Long, K As Long, UR As Long, Somma As Long, Valore As Integer
Sub Somma_per_Valore()
'Cancella le "Somme" presenti nella Colonna "C"
UR = Range("C" & Rows.Count).End(xlUp).Row
Range("C1:C" & UR).ClearContents
' In "D1" c'è il valore che indica quante celle sommare
Valore = [D1]
J = 1
K = 1
Somma = 0
' Nella Colonna "B" ci sono i Valori da sommare
UR = Range("B" & Rows.Count).End(xlUp).Row
For I = 1 To UR
If J <= Valore Then
Somma = Somma + Cells(I, 2)
J = J + 1
Else
' Nella Colonna "C" vengono scritte le somme
Cells(K, 3) = Somma
K = K + 1
J = 2
Somma = Cells(I, 2)
End If
Next I
If J = Valore Then
Cells(K, 3) = Somma
Else
MsgBox "I dati presenti non sono un multiplo intero del Valore: " & Valore & Chr(13) _
& Chr(13) & " La somma è stata eseguita fino all'ultimo multiplo intero"
End If
End Sub |