Loop Question

kevinh2320

Board Regular
Joined
May 13, 2016
Messages
61
I need code that will loop through my work sheet and if there is only a single entry for an Account# then leave that record. But, if there is multiply rows with that account number delete all rows with that account number except the most recently entered row based on the TransactionDate. Example worksheet below - the highlighted rows represent the records to keep. Thx for any help.

1607473398225.png
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
First two suggestions to help get faster/better solutions from the forum
  • Update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
  • Investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.
You shouldn't need to loop each row to achieve this, Try this with a copy of your data.

VBA Code:
Sub KeepLatest()
  With Range("A1", Range("B" & Rows.Count).End(xlUp))
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(2), Order2:=xlDescending, Header:=xlYes
    .RemoveDuplicates Columns:=1, Header:=xlYes
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,109
Members
449,205
Latest member
ralemanygarcia

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