Hi All,
I have 4 columns in my spreadsheet. I am trying to find any duplicates that may exist in Col D, sum values in Col C and concatenate corresponding values in Col B , then delete the entire row.
I have found VBA code on MRExcel website but honestly don't know how to tweak this code as per my requirements.
Any help that you can provide would be most appreciative.
I have attached data sample illustrating before and after as picture.
Before:
Col A Col B Col C Col D
After:
Col A Col B Col C Col D
Sub Test()
Dim Sh As Worksheet
Dim LastRow As Long
Dim Rng As Range
Set Sh = Worksheets(1)
Sh.Columns(5).Insert
LastRow = Sh.Range("A65536").End(xlUp).Row
With Sh.Range("A1:A" & LastRow).Offset(0, 4)
.FormulaR1C1 = "=IF(COUNTIF(R1C[-4]:RC[-4],RC[-4])>1,"""",SUMIF(R1C[-4]:R[" & LastRow & "]C[-4],RC[-4],R1C[-1]:R[" & LastRow & "]C[-1]))"
.Value = .Value
End With
Sh.Columns(4).Delete
Sh.Rows(1).Insert
Set Rng = Sh.Range("D1:D" & LastRow + 1)
With Rng
.AutoFilter Field:=1, Criteria1:="="
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
I have 4 columns in my spreadsheet. I am trying to find any duplicates that may exist in Col D, sum values in Col C and concatenate corresponding values in Col B , then delete the entire row.
I have found VBA code on MRExcel website but honestly don't know how to tweak this code as per my requirements.
Any help that you can provide would be most appreciative.
I have attached data sample illustrating before and after as picture.
Before:
Col A Col B Col C Col D
AccountingGroup | Mon 06-Dec-21 | ||
321_AFS | PRODUCT 1 | 39,400.00 | XS2395583995 |
321_AFS | PRODUCT 2 | 67,800.00 | XS2395583995 |
321_AFS | PRODUCT 3 | 67,800.00 | XS2395583995 |
321_AFS | PRODUCT 4 | 29,500.00 | XS2395584456 |
321_AFS | PRODUCT 5 | 67,400.00 | XS2395584456 |
321_AFS | PRODUCT 6 | 2,000.00 | XS2395584456 |
321_AFS | PRODUCT 7 | 9,100.00 | XS2395584456 |
After:
Col A Col B Col C Col D
AccountingGroup | Mon 06-Dec-21 | ||
321_AFS | PRODUCT 1, PRODUCT 2, PRODUCT 3 | 175,000.00 | XS2395583995 |
321_AFS | PRODUCT 4, PRODUCT 5, PRODUCT 6, PRODUCT 7 | 108,000.00 | XS2395584456 |
Sub Test()
Dim Sh As Worksheet
Dim LastRow As Long
Dim Rng As Range
Set Sh = Worksheets(1)
Sh.Columns(5).Insert
LastRow = Sh.Range("A65536").End(xlUp).Row
With Sh.Range("A1:A" & LastRow).Offset(0, 4)
.FormulaR1C1 = "=IF(COUNTIF(R1C[-4]:RC[-4],RC[-4])>1,"""",SUMIF(R1C[-4]:R[" & LastRow & "]C[-4],RC[-4],R1C[-1]:R[" & LastRow & "]C[-1]))"
.Value = .Value
End With
Sh.Columns(4).Delete
Sh.Rows(1).Insert
Set Rng = Sh.Range("D1:D" & LastRow + 1)
With Rng
.AutoFilter Field:=1, Criteria1:="="
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub