
Option Explicit
Sub somma_strutture()
Dim s As String, cella As Range, somma As Integer, table As Range, r As Range
Set table = Range(Cells(2, "A"), Cells([COUNTA(A:A)], "A"))
[a1].Sort key1:=[B:B], header:=xlYes
For Each cella In table
somma = 0
If Val(cella.Offset(, 2)) = 0 Then
[a1].AutoFilter field:=1, Criteria1:=cella & "*"
For Each r In table
If Not r.EntireRow.Hidden Then
somma = somma + r.Offset(, 2)
End If
Next
cella.Offset(, 2) = somma
End If
Next
[a1].AutoFilter
[a1].Sort key1:=[A:A], header:=xlYes
MsgBox "Fatto."
End Sub |
Option Explicit
Sub somma_strutture()
Dim s As String, cella As Range, somma As Integer, table As Range, r As Range, v As Variant
Dim s1 As Integer, s2 As Integer
Set table = Range(Cells(2, "A"), Cells([COUNTA(A:A)], "A"))
[a1].Sort key1:=[B:B], header:=xlYes
For Each cella In table
somma = 0
If Val(cella.Offset(, 2)) = 0 Then
[a1].AutoFilter field:=1, Criteria1:=cella & "*"
For Each r In table
If Not r.EntireRow.Hidden Then
somma = somma + r.Offset(, 2)
End If
Next
cella.Offset(, 2) = somma
End If
Next
[a1].AutoFilter
[a1].Sort key1:=[A:A], header:=xlYes
Names.Add Name:="table", RefersTo:=table
For Each v In Array("A.R", "A.S", "A.T", "A.V")
s1 = s1 + Evaluate("SUMIF(table," & Chr(34) & v & Chr(34) & ",OFFSET(table,0,2))")
Next
For Each v In Array("A.A", "A.C", "A.D", "A.F", "A.P", "A.Q")
s2 = s2 + Evaluate("SUMIF(table," & Chr(34) & v & Chr(34) & ",OFFSET(table,0,2))")
Next
table.Offset(-1).Find("A").Offset(, 2) = s1 - s2
Names("table").Delete
MsgBox "Fatto."
End Sub |
