Help with sequential counting

asjmoron

Board Regular
Joined
Apr 26, 2016
Messages
98
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I think I may have found something that can not be done!

Basically, I have the below (this spans circa 6k rows but this is just a snippet)


kf_Client_Reference_NumberFrom_Date
0011
10/11/2014​
0011
28/02/2014​
0014
22/08/2016​
0014
07/12/2015​
0014
14/08/2014​
0014
06/09/2013​
0016
19/09/2016​
0016
02/09/2015​
0016
02/09/2014​
0016
17/09/2013​


I am trying to trim this data to nly keep the two most recent records based on reference number. So, the above would then be


kf_Client_Reference_NumberFrom_Date
0011
10/11/2014​
0011
28/02/2014​
0014
22/08/2016​
0014
07/12/2015​
0016
19/09/2016​
0016
02/09/2015​


I hope that makes sense?

Thanks in advance for any advice
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
What version of excel are you using?
I suggest that you update your Account details (or 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’)
 
Upvote 0
Another thing, will the dates in col B always be sorted newest to oldest?
 
Upvote 0
Another thing, will the dates in col B always be sorted newest to oldest?
Im using 2016 (sorry it's been a while since I've been here!).

In the interest of trying to make this as painless as possibe, yes, I have the column ordered Newest to Oldest.
 
Upvote 0
If your data is sorted like that could you use this formula copied down then filter column C for "1" values and delete those rows?

21 08 23.xlsm
ABC
1kf_Client_Reference_NumberFrom_DateCheck
21111/10/2014 
31128/02/2014 
41422/08/2016 
51412/07/2015 
61414/08/20141
7149/06/20131
81619/09/2016 
9169/02/2015 
10169/02/20141
111617/09/20131
Keep 2
Cell Formulas
RangeFormula
C2:C11C2=IF(COUNTIF(A$2:A2,A2)<3,"",1)
 
Upvote 0
Try, this.
VBA Code:
Sub test()

    With Sheets("Sheet1")
        
        With .Range("C2:C" & .Cells(Rows.Count, 1).End(3).Row)
            .Formula = "=COUNTIF(A$2:A2,A2)"
            .Value = .Value
        End With

        If .AutoFilterMode = False Then .Rows(1).AutoFilter
        .Range("A1").CurrentRegion.AutoFilter 3, ">2"
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        .AutoFilter.ShowAllData
        .Range("C:C").ClearContents
        .AutoFilterMode = False
    
    End With

End Sub
 
Upvote 0
If you want to use a macro and there are a reasonable number of disjoint rows to be deleted out of your 6k rows then I think you would find this noticeably faster.
I have assumed the data in columns A:B, column C empty and that there will be at least one row to delete. Code can be adapted if any of that may not be the case.

VBA Code:
Sub Keep_Two()
  With Range("C4:C" & Range("A" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[-2]=R[-2]C[-2],1,"""")"
    .Value = .Value
    .Offset(, -2).Resize(, 3).Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
    .SpecialCells(xlConstants, xlNumbers).EntireRow.Delete
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

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