删除Excel中巨大的数据表中只有一些行的function?

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

 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 

在这里输入图像说明

注意:如果你有很多数据,我build议在Sub的开始处将Application.ScreenUpdating改为False ,将Application.Calculation改为xlCalculationManual ,然后在Sub的末尾改回它,这样可以使性能更好。

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

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

在这里输入图像说明