2016-07-26 74 views
2

每周我收到一个电子表格,最多可包含4,000行。如果任何日期在列C中,我需要删除该行及其上方的行。有人可以帮我提供一些VBA代码,我可以在每周获得的每个新文件上运行。每周手工处理4000多行数据是一种痛苦。Excel - 根据单元格值删除当前行和上面的行

在截图中,我会删除行157,158,159和160
Screenshot showing rows to be deleted

非常赞赏的帮助。

回答

3

给这个短宏一试:

Sub dural() 
    Dim N As Long, i As Long, v As Variant 

    N = Cells(Rows.Count, "C").End(xlUp).Row 

    For i = N To 2 Step -1 
     v = Cells(i, "C").Value 
     If v <> "" Then 
      If IsDate(v) Then 
       Range(Cells(i, "C"), Cells(i - 1, "C")).EntireRow.Delete 
      End If 
     End If 
    Next i 
End Sub 

编辑#1:

如果有两个或两个以上连续的细胞像日期:

enter image description here

然后应使用此代码:

Sub dural() 
    Dim N As Long, i As Long, v As Variant 
    Dim rKill As Range 
    N = Cells(Rows.Count, "C").End(xlUp).Row 
    Set rKill = Nothing 
    For i = N To 2 Step -1 
     v = Cells(i, "C").Value 
     If v <> "" Then 
      If IsDate(v) Then 
       If rKill Is Nothing Then 
        Set rKill = Cells(i, "C") 
        Set rKill = Union(rKill, Cells(i - 1, "C")) 
       Else 
        Set rKill = Union(rKill, Cells(i, "C")) 
        Set rKill = Union(rKill, Cells(i - 1, "C")) 
       End If 
      End If 
     End If 
    Next i 

    If rKill Is Nothing Then Exit Sub 
    rKill.EntireRow.Delete 
End Sub 

(这将确保所有的 “X” 行也被删除。)

+0

工作就像一个魅力。非常感谢你! – Steve

+0

@Steve查看我的**编辑#1 ** –

+0

良好的通话。我有时在列C中有两行或更多行的日期..... – Steve

相关问题