2016-02-07 133 views
1

我正在过滤一个表加载赛马信息。我已经完成了8个过滤器中的7个,但是我正在为最后一个过滤器而挣扎。基本上全部我需要的是看比赛时间栏(col B),如果有2次相同的话显然在比赛中有2个选择。我只想要一个选择,所以接下来我想删除/筛选基于C列的培训师胜利%。无论哪一个更大的百分比是我需要保留的百分比。如果他们是相同的,也许把msgbox说出来,否则我可能不得不过滤第三个字段。我发现了一段越来越近的代码。删除一行,如果重复,然后在另一列上过滤

Sub text()  
Dim j As Integer, k As Integer, r As Range  
j = Range("E2").End(xlDown).Row  
For k = j To 2 Step -1  
    MsgBox k  
    Set r = Range(Cells(k, "E"), Cells(k, "E").End(xlUp))  
    If WorksheetFunction.CountIf(r, Cells(k, "E")) > 1 Then  
     Cells(k, "E").EntireRow. Delete  
    End If  
Next k  
End Sub 
+0

请格式化您的代码。 – phoenix

回答

1

排序教练赢得比例(降序)的数据,然后在时间上使用Range.RemoveDuplicates method

Sub tract() 
    With Worksheets("Sheet6") '<~~ you should know which worksheet you are on 
     With .Cells(1, 1).CurrentRegion 
      'sort on the trainer win percentage (descending) 
      .Cells.Sort Key1:=.Columns(3), Order1:=xlDescending, _ 
         Orientation:=xlTopToBottom, Header:=xlYes 
      'remove duplicate times - the lower rows will be deleted 
      .RemoveDuplicates Columns:=2, Header:=xlYes 
     End With 
    End With 
End Sub 

删除重复始终去除下进入;保持入口接近顶部。这就是为什么排序培训师首先赢得百分比很重要。这不包括胜利百分比也是重复的情况。在这种情况下,必须添加另一个排序键。如果Z列是决定性因素,并且您希望保留较低的值,那么它将类似于以下内容。

Sub tract() 
    With Worksheets("Sheet6") '<~~ you should know which worksheet you are on 
     With .Cells(1, 1).CurrentRegion 
      'sort on the trainer win percentage (descending) and columns Z (ascending) 
      .Cells.Sort Key1:=.Columns(3), Order1:=xlDescending, _ 
         Key2:=.Columns(26), Order2:=xlAscending, _ 
         Orientation:=xlTopToBottom, Header:=xlYes 
      'remove duplicate times - the lower rows will be deleted 
      .RemoveDuplicates Columns:=2, Header:=xlYes 
     End With 
    End With 
End Sub 
+0

Jeeped这是完美的工作。希望我一周前问过。干杯 – newmas

+0

嗨。我刚刚发现,当我在过滤后运行上面的代码时,它并不总是给出正确的结果。它会查看“隐藏的”过滤项目并删除较低的培训师比例。是否有一种方法只对可见的记录执行代码,而不是与隐藏的过滤记录进行比较? – newmas

+0

[Range.RemoveDuplicates方法](https://msdn.microsoft.com/en-us/library/office/ff193823.aspx)不限于可见单元格,但还有其他方法只能处理已过滤的集合。建议你关闭这个问题,并开始另一个恐吓这成为一个[俄罗斯娃娃问题](http://meta.stackexchange.com/questions/188625/etiquette-for-russian-doll-questions)。 – Jeeped

相关问题