Cada linha de código é comentada. Por isso é fácil adaptar o código para tarefas semelhantes
O que faz
- Essa macro VBA combina todas as linhas exclusivas em uma planilha copiada.
Ele analisa os valores da coluna A para decidir qual coluna é um dublicado - Soma as colunas B, C, F e H.
- calcula a média para D, E, G, I e J.
Ele não usa uma média ponderada, pois eu ainda não sei exatamente como você os calcula
Como usar
- abra sua pasta de trabalho de dados e pressione ALT + F11
- copiar & cole o código abaixo em algum lugar ou em um novo modul
- personalize
AVcols()
eSUMcols()
se você quiser calcular a soma ou a média em outras colunas - feche o editor do VBA e selecione / visualize a planilha que deseja combinar
- pressione ALT + F8 e execute a macro
combineduplicates
Sub combineduplicates() '### starts our macro
Application.ScreenUpdating = False '### Excel wont update its screen while executing this macro. This is a huge performace boost
Dim AVcols() '### declare an empty array for our average columns
Dim SUMcols() '### declare a second empty array for our sum columns
Dim AVtemp() '### declare a third empty array for our temporal values we need to calculate a weighted average
AVcols() = Array(4, 5, 7, 9, 10) '### we use the first array to store our columns for calculating an average
SUMcols() = Array(2, 3, 6, 8) '### the second array stores the columns which should be summed up
Mcol = 2 '### whats the multiplier column for our weighted average?
ActiveSheet.Copy Before:=Sheets(1) '### take a copy of our activesheet. this way we don't touch the original data
'### the next line sets our range for searching dublicates. Starting at cell A2 and ending at the last used cell in column A
Set searchrange = Range([A2], Columns(1).Find(what:="*", after:=[A1], searchdirection:=xlPrevious))
For Each cell In searchrange '### now we start looping through each cell of our searchrange
ReDim AVtemp(UBound(AVcols) + 1, 0) '### make our temp array 2-dimensional and reser it from the previous loop
For i = 0 To UBound(AVcols) '### save values from start row for average calculating into the temp array
AVtemp(i, UBound(AVtemp, 2)) = CDbl(Cells(cell.Row, AVcols(i))) '### still filling the temp array
Next i '### go ahead to the next column
AVtemp(UBound(AVcols) + 1, UBound(AVtemp, 2)) = CDbl(Cells(cell.Row, Mcol)) '### save the clicks too
Set search = searchrange.Find(cell, after:=cell, lookat:=xlWhole) '### searches for a dublicate. If no dub exists, it finds only itself
Do While search.Address <> cell.Address '### until we find our starting cell again, these rows are all dublicates
For i = 0 To UBound(SUMcols) '### loop through all columns for calculating the sum
'### next line sums up the cell in our starting row and its counterpart in its dublicate row
Cells(cell.Row, SUMcols(i)) = CDbl(Cells(cell.Row, SUMcols(i))) + CDbl(Cells(search.Row, SUMcols(i)))
Next i '### go ahead to the next column
ReDim Preserve AVtemp(UBound(AVcols) + 1, UBound(AVtemp, 2) + 1) '### expand the temp array so we have enough space to fill with values
For i = 0 To UBound(AVcols) '### loop through all columns for calculating the weighted average
'### the next line saves the value in our temp array, but now for the duplicate rows
AVtemp(i, UBound(AVtemp, 2)) = CDbl(Cells(search.Row, AVcols(i)))
Next i '### go ahead to the next column
AVtemp(UBound(AVcols) + 1, UBound(AVtemp, 2)) = CDbl(Cells(search.Row, Mcol)) '### save the clicks too
search.EntireRow.Delete '### we are finished with this row. Delete the whole row
Set search = searchrange.Find(cell, after:=cell) '### and search the next dublicate after our starting row
Loop
If search.Row = cell.Row Then '### ok, now we have to calculate the average. All needed values are temporarly stored in our temp array
For i = 0 To UBound(AVcols) '### start with looping through all average columns
average = 0 '### reset the variable from the last loop
For j = 0 To UBound(AVtemp, 2) '### start looping through the data from all dublicated rows
clicks = AVtemp(UBound(AVcols) + 1, j) '### take the clicks for that row from the array
sumclicks = Cells(cell.Row, Mcol) '### take the summed up clicks for all dublicated rows
addaverage = AVtemp(i, j) '### take the value which should be multiplied
average = average + (clicks / sumclicks * addaverage) '### now calculate the weighted average and sum it up with the old one
Next j '### goto next data of dublicate rows
Cells(cell.Row, AVcols(i)) = average '### when finished with calculating, write the result to the workbook
Next i '### go ahead to the next average column
End If '### only the end line of our condition
Next '### from here we start over with the next cell of our searchrange
'### Note: This is a NEW unique value since we already deleted all old dublicates
Application.ScreenUpdating = True '### re-enable our screen updating
End Sub '### ends our macro
Dê uma olhada na minha pasta de trabalho de teste se tiver problemas para iniciar a macro.