Excel 查找重复数据并删除最旧的数据

我有一个MS Excel中的问题。 我有一个电子表格,里面有这样的数据:

Name | timestamp ------------------------ Smith | 12.05.2015 Smith | 01.01.2015 Smith | 10.05.2015 Simpson | 14.04.2015 Simpson | 10.02.2015 Simpson | 21.03.2015 Simpson | 02.01.2015 

我的数据更大更复杂,有不同时间戳的重复。 现在我想删除旧的,并希望这样的输出:

  Name | timestamp Smith | 12.05.2015 Simpson | 14.04.2015 

我知道如何删除重复,但在这种情况下,它有点不同。 我希望你能帮我解决这个问题。

你可能不需要VBA。

根据我的经验,Excel删除重复代码的作用是删除列表中遇到的第一个重复项。

所以按照名称升序和时间戳降序对数据进行sorting,然后仅从名称字段中删除重复项。

你应该留下最近的名字。

我做了一些testing,并且Range.RemoveDuplicates似乎保留每个重复值的第一个条目(至less在您要使用的sorting范围内)。 这是我的解决scheme:

 Sub SortAndCondense() 'This subroutine sorts a table by name and secondarily by descending date. It then removes 'all duplicates in the name column. By sorting the dates in descending order, only the most 'recent entries for each name are preserved Dim wrkSht As Worksheet Set wrkSht = ThisWorkbook.Worksheets("Sheet1") Dim dateTable As Range Dim header1 As Range, header2 As Range Set dateTable = wrkSht.Range("A2:B7") 'insert your actual table range; modify as necessary for column headers Set header1 = wrkSht.Range("A2") Set header2 = wrkSht.Range("B2") 'sort the column primarily by name, and secondarily by descending date. The order in which the names are sorted 'is irrelevant. dateTable.Sort Key1:=header1, Key2:=header2, Order2:=xlDescending 'remove all duplicate names. The way remove duplicates works (so far as I can tell) is that it keeps only the 'topmost entry for each duplicate column. dateTable.RemoveDuplicates 1 End Sub