2017-10-28 175 views
0

我需要你帮忙解决我在Excel 2016中遇到的这个问题(英文)。只能删除Excel中巨大的数据表中的某些行的功能?

我havea大量的数据,结构是这样的:

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行的表,所以我不能每次都手动执行它,这就是为什么我需要一个函数/宏来做到这一点。希望你能帮助我!

回答

0
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 

enter image description here

注意:如果你有大量的数据,我建议改变Application.ScreenUpdatingFalseApplication.CalculationxlCalculationManual在小组开始,然后改变它回到了Sub的结尾,因为这会使性能更好。

+0

非常感谢!这是完美的 – Luke

1

使用下面的公式来计算D2。将公式向下拖动,过滤FALSE值并删除。

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

enter image description here

+0

非常感谢您的意见。但是,您的解决方案需要太多的步骤/点击次数,这就是为什么我最喜欢的解决方案是由@Tigregalis提出的解决方案 – Luke

+0

没问题,但是它的价值。我建议你避免使用VBA,除非它真的有必要。 – CallumDA