Delete a row if the date does not match

倖福魔咒の 提交于 2020-01-06 11:01:58

问题


I've got about 20,000 lines of data ordered by date. Sometimes, there's a time mismatch (ie one time series is quoted more frequently than the other). I want to delete the row starting at column D if the time does not match that of column A.

I've written the following code, but I'm getting errors (runtime error 1004). I'm not sure where's the problem. I think it might be the WorksheetFunction.

Dim rng As Range
Dim c As Variant
Dim myVal As Boolean

Sub rectifyData()
    Set rng = Range("A4:A20459")
    For Each c In rng.Rows
        myVal = xlApp.WorksheetFunction.If(c = ActiveSheet.Range(c).Offset(0, 4), 0, 1)
        If (myVal = 1) Then
            ActiveSheet.Range(c).Offset(0, 4).Rows.Delete
            myVal = 0
        End If
    Next c
End Sub

回答1:


Perhaps:

Sub RowKiller3()
    Dim rDel As Range
    Dim r As Range, rBig As Range

    Set rBig = Range("A4:A20459")
    Set rDel = Nothing
    For Each r In rBig
        If r.Value <> r.Offset(0, 3) Then
            If rDel Is Nothing Then
                Set rDel = r
            Else
                Set rDel = Union(rDel, r)
            End If
        End If
    Next r

    If Not rDel Is Nothing Then
        rDel.EntireRow.Delete
    End If

End Sub


来源:https://stackoverflow.com/questions/22806551/delete-a-row-if-the-date-does-not-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!