
Sub Somma_se_VBA()
Dim r As Long
Dim i As Long, j As Variant
Dim ka, kb, k()
Dim x As Variant
x = 12.5 'valore da cercare
r = Range("A" & Rows.Count).End(3).Row
ka = Range("A1:B" & r) 'Range di una o più colonne
kb = Range("C1:C" & r) 'nota: Somma i valori da una sola colonna!
ReDim k(1 To UBound(ka, 1), 1 To 1)
j = 0
For i = 1 To UBound(ka, 1)
If ka(i, 1) = x Then
j = kb(i, 1) + j
k(i, 1) = j
End If
Next
'risultato in colonna E
Range("E1").Resize(UBound(k, 1)) = k
Cells(UBound(k, 1), 6) = j 'Risultato Finale
End Sub
|
'=MyFunc(A1:B10;3,1;"C")
Function MyFunc(RangeA As Range, MyArg1 As Variant, ColX As String)
For Each celle In RangeA
If celle.Value = MyArg1 Then
MyFunc = MyFunc + Cells(celle.Row, ColX).Value
End If
Next
End Function |
Option Explicit
Function somma_condizionale(intervallo As Range, criterio As Variant, Optional int_somma As Range)
Dim cella As Range, somma As Long, i As Long, intervallo_somma As Range
If int_somma Is Nothing Then
Set intervallo_somma = intervallo
Else
Set intervallo_somma = int_somma
End If
For Each cella In intervallo
i = i + 1
If cella = (criterio) Then
somma = somma + intervallo_somma(i)
End If
Next
somma_condizionale = somma
End Function
|
