【问题标题】:Function to remove only some rows among huge table of data in Excel?仅删除Excel中庞大数据表中的某些行的功能?
【发布时间】:2018-04-09 19:57:36
【问题描述】:

我在 Excel 2016 中遇到的这个问题需要您的帮助(英文)。

我有很多数据,结构如下:

code_red    value_1
code_green  value_2
code_green  value_3
code_green  value_4
code_green  value_5
code_blue   value_6

我需要使用一个可以让我得到的函数:

code_red    value_1
code_green  value_2
code_green  value_5
code_blue   value_6

我尝试使用 Remove duplicate,但我需要保留重复项中的最后一行。

此外,我需要稍后在我的数据中也适用相同的原则,所以假设我有:

code_black  value_101
code_green  value_102
code_green  value_103
code_green  value_104
code_yellow value_105

我需要能够获得:

code_black  value_101
code_green  value_102
code_green  value_104
code_yellow value_105

请记住,我有一个超过 2000 行的表格,所以我不能每次都手动执行,这就是我需要一个函数/宏来执行此操作的原因。希望你能帮我解决这个问题!

【问题讨论】:

    标签: vba excel duplicates filtering


    【解决方案1】:

    D2 中使用以下公式。向下拖动公式,过滤FALSE 值并删除。

    =OR(A2<>A1,A2<>A3)
    

    【讨论】:

    • 非常感谢您的意见。但是,您的解决方案需要太多的步骤/点击,这就是为什么我最喜欢的解决方案是 @Tigregalis 提出的解决方案
    • 没问题,但物有所值。我建议您避免使用 VBA,除非确实有必要。
    【解决方案2】:
    Sub RemoveIntermediateRows()
    
        Dim R As Range, Rstart As Range
    
        Set R = ActiveSheet.Range("A1") 'set initial range
        Set Rstart = R ' set the start of the value range (equal to the initial range at first)
    
        Do Until R = "" 'loop termination condition; we are assuming contiguous rows of data here, and it ends at a blank value
    
            ' if we have a new value
            If R.Value <> Rstart.Value Then ' (this will always be false on the first row of course)
            ' then R(0) is the end of the previous value range,
            ' and Rstart is the start of the previous value range
    
                ' if there is at least 1 row between R(0) and Rstart
                ' then build a range of those intermediate rows (one row before R(0), one row after Rstart)
                ' and delete them
                If R(0).row - Rstart.row > 1 Then Range(R(-1), Rstart(2)).EntireRow.Delete
    
                Set Rstart = R ' set the start of the new value range
            End If
    
            Set R = R(2) 'move to next row
        Loop
    
    End Sub
    

    注意:如果你有很多数据,我建议在Sub的开头将Application.ScreenUpdating更改为FalseApplication.CalculationxlCalculationManual,然后在Sub的末尾将其改回,因为这将使性能更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 2021-01-16
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2012-10-14
      • 2015-02-06
      • 1970-01-01
      相关资源
      最近更新 更多