2017-10-28 171 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