Hi all,
I recently obtained some code to summarise duplicate values in a range, whereby in a 2 column 2 row range, if the values in the first column where the same, the values in the second column would be added. (Incidentally, I have forgotten the author of this helpful code. My apologies to whoever you may be.)
Anyhow, I was hoping someone could please assist in altering this code for me.
First of all, rather than add all of the columns except column one, I was hoping to summarise only columns 5-7.
And secondly, I was hoping to also check for duplicates in column 4. So if duplicates existing in the first column, the macro must then check column 4 for duplicates before summarising.
I realise this may not make as much sense as I would like, so I have attached a simple example spreadsheet.
Here is the code that I have at the moment. I would very grateful if someone can help.
Thank you
[VBA]Sub SummariseDuplicates()
Dim cel As Range, rg1 As Range, rg2 As Range
Dim ckValue As String, i As Long, j As Long
Dim wb1 As Workbook, ws1 As Worksheet
Dim vcels As Variant, ditem As Variant, vHeader As Variant
Dim DuplicatesDic As Object
Set DuplicatesDic = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
Set wb1 = ActiveWorkbook
Set ws1 = ActiveSheet
Set rg1 = Intersect(Selection.Columns(1).EntireColumn, ws1.UsedRange).Offset(1)
vHeader = Intersect(ws1.Rows(1).EntireRow, ws1.UsedRange).Value
For Each cel In rg1
ckValue = LCase(cel)
If ckValue <> "" Then
Set rg2 = Intersect(cel.EntireRow, ws1.UsedRange)
If Not DuplicatesDic.exists(ckValue) Then
vcels = rg2
DuplicatesDic.Add ckValue, vcels
Else
vcels = DuplicatesDic(ckValue)
For i = LBound(vcels, 2) To UBound(vcels, 2)
If IsNumeric(vcels(1, i)) And IsNumeric(rg2.Cells(i)) And i <> cel.Column Then
vcels(1, i) = vcels(1, i) + rg2.Cells(i)
End If
Next i
DuplicatesDic.Remove ckValue
DuplicatesDic.Add ckValue, vcels
End If
End If
Next cel
ws1.Cells.Clear
ws1.Range(Cells(1, 1), Cells(1, UBound(vHeader, 2))) = vHeader
i = 1
For Each ditem In DuplicatesDic.Items
i = i + 1
ws1.Range(Cells(i, 1), Cells(i, UBound(ditem, 2))) = ditem
Next ditem
Application.ScreenUpdating = True
Set DuplicatesDic = Nothing
End Sub[/VBA]