Option Explicit
Sub copia_senza_ripetizione()
'routine del sito
Dim Elenco As New Collection
Dim IntervOrig As Range
Dim IntervDest As Range
Dim Riga, R1, Codice, Tot1, Tot2, Tot3, Tot4
' Worksheets("Foglio1").Select
' definisco l'intervallo di origine
With Range("A1").CurrentRegion
Set IntervOrig = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
End With
' IntervOrig.Select
' creazione della collection dei dati univoci
' nella colonna 2 stanno i dati che si vogliono rendere univoci
On Error Resume Next
For Riga = 1 To IntervOrig.Rows.Count
Worksheets("foglio1").Cells(1, 14) = Worksheets("foglio1").Cells(1, 14) + 1
Elenco.Add IntervOrig(Riga, 2).Value, CStr(IntervOrig(Riga, 2).Value)
IntervOrig(Riga, 9).Value = Worksheets("foglio1").Cells(1, 14)
IntervOrig(Riga, 10).Value = Date
Next
On Error GoTo 0
' definisco l'intervallo di destinazione che è subito sotto le intestazioni di colonna
' ATTENZIONE A QUESTO
Set IntervDest = Range("A30:E30").Offset(1, 0)
' IntervDest.Select
' debbo sommare i dati relativi a
' qta posa importo posa qta forn importo forn
' che nella tabella originale stanno nelle colonne 5, 6, 7, 8
' e che nella tabella di destinazione sono nelle colonne 2, 3, 4, 5
For Riga = 1 To Elenco.Count
Codice = Elenco(Riga)
IntervDest(Riga, 1) = Codice
Tot1 = 0: Tot2 = 0: Tot3 = 0: Tot4 = 0
For R1 = 1 To IntervOrig.Rows.Count
If IntervOrig(R1, 2) = Codice Then
Tot1 = Tot1 + IntervOrig(R1, 5)
Tot2 = Tot2 + IntervOrig(R1, 6)
Tot3 = Tot3 + IntervOrig(R1, 7)
Tot4 = Tot4 + IntervOrig(R1, 8)
End If
Next
IntervDest(Riga, 2) = Tot1
IntervDest(Riga, 3) = Tot2
IntervDest(Riga, 4) = Tot3
IntervDest(Riga, 5) = Tot4
Next
End Sub |