macro to condense data.

awsumchillicrab

Board Regular
Joined
Jan 30, 2011
Messages
56
Hi Excel people.

I've got something like this:
Column A showing grocery items, and they can repeat.
Column B showing the number of that item I need.
Column C showing the number of that item I bought in a transaction.

If I need 10 cucumbers and have bought 7 so far in two transactions...

A2 and A10 may say "cucumber"
B2 and B10 will both be "10"
C2 and C10 may be "3" and "4" respectively.

I'm trying to make a macro that will add 3+4, then replace C2 with "7" and delete the entire row10. So that at the end of the macro, "cucumber" will only ever show up once in column A.

If i buy 2 more cucumbers today, I'd like to add the transaction at the bottom of my table, re-run the macro, and have C2 showing "9".

Any help will be great, thanks!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this with a copy of your sheet.

Code:
Sub test2()
Dim LR As Long, i As Long, s As String
Application.ScreenUpdating = False
s = Range("C1").Value
LR = Range("A" & Rows.Count).End(xlUp).Row
Columns("D").Insert
With Range("D2:D" & LR)
    .Formula = "=SUMIF(A:A,A2,C:C)"
    .Value = .Value
End With
For i = LR To 3 Step -1
    If WorksheetFunction.CountIf(Columns("A"), Range("A" & i).Value) > 1 Then Rows(i).Delete
Next i
Columns("C").Delete
Range("C1").Value = s
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I assumed that row 1 contained headers. Therefore you only need to go up as far as row 3 to check for duplicates.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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