Add a new row based on Unique value and delete old

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
Hi All,

First time I am coming to this situation were I don't have any idea how to write a macro for below situation.

1) Unique id is in Column B.
2) Amount is in column C.
3) date of transaction is in column A

I need a macro which will add the amount based on unique ID and create new rows which will have the following and delete old rows.
- last date in date column
- ID in particulars column
- amount in amount column.

DateParticularsAmount
23-Jul-18ABC -3,36,000
24-Jul-18ABC 78,000
25-Jul-18ABC 48,000
26-Jul-18ABC 54,000 -1,56,000
23-Jul-18XYZ -3,36,000
24-Jul-18XYZ 2,27,800
25-Jul-18XYZ 98,000
25-Jul-18XYZ 1,000
26-Jul-18XYZ 54,000 44,800

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

my output should be as shown below:

DateParticularsAmount
26-Jul-18ABC-156000
26-Jul-18XYZ44800

<colgroup><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about
Code:
Sub SumDupes()
   Dim Cl As Range
   Dim Tmp1 As Variant, Tmp2 As Variant
   Dim Itm As Variant
   Dim i As Long
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Array(Cl.Offset(, -1).Value, Cl.Value, Cl.Offset(, 1).Value)
         Else
            Tmp2 = .Item(Cl.Value)(2) + Cl.Offset(, 1).Value
            If .Item(Cl.Value)(0) < Cl.Offset(, -1).Value Then Tmp1 = Cl.Offset(, -1).Value Else Tmp1 = .Item(Cl.Value)(0)
            .Item(Cl.Value) = Array(Tmp1, Cl.Value, Tmp2)
         End If
      Next Cl
      Range("A1").CurrentRegion.Offset(1).ClearContents
      i = 1
      For Each Itm In .items
         i = i + 1
         Range("A" & i).Resize(, 3).Value = Itm
      Next Itm
   End With
End Sub
 
Upvote 0
My data columns have increased I have edited the code however I am not getting the output in correct column...
- product id coming in column2(it should come (below product)
- Quantity coming in column3(it should come (below Quantity)

Output
Transaction DateColumn2Column3ProductColumn5Column6Column7Column8Column9Column10Column11Column12QuantityColumn14
26-Jul-18ABC-156000#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A
26-Jul-18XYZ159300#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A#N/A
New Data
Transaction DateColumn2Column3ProductColumn5Column6Column7Column8Column9Column10Column11Column12QuantityColumn14
23-Jul-18ABC-336000
24-Jul-18ABC78000
25-Jul-18ABC48000
26-Jul-18ABC54000
23-Jul-18XYZ-10800
24-Jul-18XYZ62100
25-Jul-18XYZ36000
26-Jul-18XYZ72000

<colgroup><col><col><col><col><col><col><col><col span="2"><col><col><col><col><col></colgroup><tbody>
</tbody>

code :
Sub SumDupes()
Dim Cl As Range
Dim Tmp1 As Variant, Tmp2 As Variant
Dim Itm As Variant
Dim i As Long
Sheets("Partly Unrealised").Select
With CreateObject("scripting.dictionary")
For Each Cl In Range("d2", Range("d" & Rows.Count).End(xlUp))
If Not .exists(Cl.Value) Then
.Add Cl.Value, Array(Cl.Offset(, -3).Value, Cl.Value, Cl.Offset(, 9).Value)
Else
Tmp2 = .Item(Cl.Value)(2) + Cl.Offset(, 9).Value
If .Item(Cl.Value)(0) < Cl.Offset(, -3).Value Then Tmp1 = Cl.Offset(, -3).Value Else Tmp1 = .Item(Cl.Value)(0)
.Item(Cl.Value) = Array(Tmp1, Cl.Value, Tmp2)
End If
Next Cl
Range("A1").CurrentRegion.Offset(1).ClearContents
i = 1
For Each Itm In .items
i = i + 1
Range("A" & i).Resize(, 13).Value = Itm
Next Itm
End With
End Sub


- could you please help

Thanks
Chandresh
 
Upvote 0
You just need to change the last part to
Code:
   For Each Itm In .items
      i = i + 1
      Range("D" & i).Value = Itm(1)
      Range("A" & i).Value = Itm(0)
      Range("M" & i).Value = Itm(2)
   Next Itm
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top