简体   繁体   English

触发日期之间的差异时,VBA 删除值

[英]VBA Delete values when difference between dates triggered

The problem is that it deletes values from both rows where the difference occurs.问题在于它从发生差异的两行中删除值。 It should delete values just from the top row where the difference occurs.它应该只从发生差异的顶行删除值。

So I tried replacing ws.Cells(RowNo, 3) = " " with ws.Cells(FirstDate, 1) = " " but it doesen't do anything.所以我尝试用ws.Cells(RowNo, 3) = " " ws.Cells(FirstDate, 1) = " "替换ws.Cells(FirstDate, 1) = " "但它没有做任何事情。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

Below is the code:下面是代码:

Sub CalculateDate()
Dim Result, RowNo As Long
Dim FirstDate, SecondDate As Date
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets(1)

RowNo = 2

    Do Until ws.Cells(RowNo + 1, 2) = ""

    FirstDate = ws.Cells(RowNo, 2)
    SecondDate = ws.Cells(RowNo + 1, 2)

        If DateDiff("d", FirstDate, SecondDate) < 2 Then
        ws.Cells(RowNo, 3) = " "
        End If

    RowNo = RowNo + 1

    Loop

End Sub

在此处输入图片说明

KEY:钥匙:

Red = where difference between 2 dates <2days红色 = 2 个日期之间的差异 <2 天

Yellow = where the cell value should be blank黄色 = 单元格值应为空的地方

Blue = value should be blank Blue = where cells should not be deleted蓝色 = 值应为空 蓝色 = 不应删除单元格的位置

may be you have to change也许你必须改变

    If DateDiff("d", FirstDate, SecondDate) < 2 Then
        ws.Cells(RowNo, 3) = " "
    End If

with

    If DateDiff("d", FirstDate, SecondDate) < 2 Then
        ws.Cells(RowNo, 3).ClearContents
        RowNo = RowNo + 1
    End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM