Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Alessandro,
Ho un file csv che ho importato in excel, si tratta di un report annuale ma il software che lo ha creato lo divide per trimestri. Il file ha circa 15000 righe per 5 colonne, dove ogni elemento è ripetuto 4 volte, una per trimestre, e l'unico campo che cambia è un numero associato ad ognuno di questi elementi relativo al determinato trimestre. Devo ottenere un report annuale sommando quindi i trimestri.
Nel dettaglio mi serve unire tutti gli elementi che hanno 4 celle uguali (delle 5 presenti per ogni elemento, si tratta di testo) e sommare la quinta colonna (numeri).
Esempio:
A B C D E
1 alfa beta gamma delta 21
2 alfa beta gamma delta 28
3 alfa beta gamma delta 23
4 alfa beta gamma omega 44
5 alfa beta gamma delta 32
6 alfa beta gamma omega 11
Siccome è possibile che alcuni elementi abbiano alcuni dati in comune con altri elementi, devo unire solo quelle che hanno effettivamente 4 celle uguali, sommando la quinta: il risultato dovrebbe essere quindi
A B C D E
1 alfa beta gamma delta 104
2 alfa beta gamma omega 55
Prova qualcosa del genereç
- Alt+F11 per aprire l'editor di VBA
- Alt+IM per inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'=========>>
Option Explicit
'--------->>
Public Sub Tester()
Dim WB As Workbook
Dim srcSH As Worksheet, destSH As Worksheet
Dim srcRng As Range, destRng As Range
Dim arrIn As Variant, arrOut As Variant
Dim arrKeys As Variant, arrItems As Variant
Dim arrJoin As Variant, arrSplit As Variant
Dim oDic As Object
Dim sStr As String, aStr As String
Dim i As Long, j As Long, k As Long
Dim ii As Long, iCtr As Long
Dim LRow As Long
Dim CalcMode As Long
Const sFoglioDati As String = "Foglio1" '<<=== Modifica
Const sFoglioDestinazione As String = "Foglio2" '<<=== Modifica
Const sDestinazione As String = "A2" '<<=== Modifica
Set WB = ThisWorkbook
With WB
Set srcSH = .Sheets(sFoglioDati)
Set destSH = .Sheets(sFoglioDestinazione)
End With
With srcSH
LRow = LastRow(srcSH, .Columns("A:A"))
Set srcRng = .Range("A1:E" & LRow)
End With
Set destRng = destSH.Range(sDestinazione)
arrIn = srcRng.Value
ReDim arrJoin(1 To 4)
Set oDic = CreateObject("Scripting.Dictionary")
With oDic
.CompareMode = vbTextCompare
For i = LBound(arrIn) To UBound(arrIn)
For j = 1 To 4
arrJoin(j) = arrIn(i, j)
Next j
sStr = Join(arrJoin, vbNewLine)
If Not .exists(sStr) Then
.Add Key:=sStr, Item:=arrIn(i, 5)
Else
.Item(sStr) = .Item(sStr) + arrIn(i, 5)
End If
Next i
iCtr = .Count
arrKeys = .keys
arrItems = .items
End With
ReDim arrOut(1 To iCtr, 1 To 5)
For k = 1 To iCtr
arrSplit = Split(arrKeys(k - 1), vbNewLine)
For ii = 1 To 4
arrOut(k, ii) = arrSplit(ii - 1)
Next ii
arrOut(k, 5) = arrItems(k - 1)
Next k
On Error GoTo XIT
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
destRng.Resize(UBound(arrOut), 5) = arrOut
Call MsgBox( _
Prompt:="Finito", _
Buttons:=vbInformation, _
Title:="REPORT")
XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
Set oDic = Nothing
End Sub
'--------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range, _
Optional minRow As Long = 1, _
Optional sPassword As String)
Dim bProtected As Boolean
With SH
If Rng Is Nothing Then
Set Rng = .Cells
End If
bProtected = .ProtectContents = True
If bProtected Then
.Unprotect Password:=sPassword
End If
End With
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
If LastRow < minRow Then
LastRow = minRow
End If
If bProtected Then
SH.Protect Password:=sPassword, _
UserInterfaceOnly:=True
End If
End Function
'<<=========
- Alt+Q per chiudere l'editor di VBA e tornare a Excel
- Salva il file con l’estensione xlsm
- Alt+F8 per aprire la finestra di gestione delle macro
- Seleziona Tester
- Esegui
Potresti scaricare il mio file di prova Alessandro20180109.xlsm
===
Regards,
Norman