2016-04-24 50 views
0

我正在處理的程序涉及讀取和確定從excel中的i - (i + 1)和i-(i-1)之間的差異。如何刪除部分數組?

如果差異超過4,程序會刪除i處的行。

該程序在第一次嘗試時效果很好。突然間,它說「你不能改變陣列的一部分」。

Option Explicit 

Sub Data_Delet() 

    Dim a As Double, b As Double, c As Double, i As Double 
    Dim rkill As Range 
    ' a,b, and c are used as steps in order to proceed to the next data points 
    a = 18 
    b = 0 
    c = 0 

    With ThisWorkbook.Worksheets("Sheet1") 

     ' The second do loop delete data points that does not follow the requirements 
     Do 
     If Abs(.Cells(a - 1, 2) - .Cells(a, 2)) > 4 And Abs(.Cells(a, 2) - .Cells(a + 1, 2)) > 4 Then 
      If rkill Is Nothing Then 
       Set rkill = Rows(a) 
      Else 
       Set rkill = Union(rkill, Rows(a)) 
      End If 
     End If 
     a = a + 1 
     Loop Until .Cells(a, 2).Value = "" 
     If Not rkill Is Nothing Then rkill.EntireRow.Delete 
    ' The third Do loop determines the number of data points that are still present after deleting the data points 
     Do 
     i = .Cells(17 + c, 1) 
     c = c + 1 
    Loop Until .Cells(17 + c, 1).Value = "" 

    ' The if statment determine whether or not the number data points from before are the same after deletion process 
    If b = c Then 
    .Cells(2, 5) = "N" 
    Else 
    .Cells(2, 5) = "Y" 
    End If 

     ' c is the number of data point after deletion 
    .Cells(12, 5) = c 

    End With 

End Sub 

回答

2

錯誤「你不能更改數組的一部分」rkill.EntireRow.Delete意味着你要刪除的行交叉的數組公式引用的範圍(用大括號的公式)。

Excel不允許這樣做。一種方法是在代碼開始時刪除違規的數組公式,然後在代碼的最後重新定義它們。或者找到將這些數組公式轉換爲正常公式的解決方案。

+0

非常感謝 – Rain