从列中删除日期

时间:2016-03-21 15:12:33

标签: excel date excel-formula

我有一个日期(A列)列,从最旧到最新排序。如果必须删除的日期在B列中,我不希望此列的日期间隔小于32天。

要理解的一个例子。

var src_img = $('img').attr('src');
src.replace('-400x280','');

在A列从2006年2月7日开始,下一个日期是11/02/2006,但它不包括在B栏中,因此必须保留。 A3是2016年2月17日,距离A2只有6天,包含在B栏中。所以A3必须去。现在,A4 = 17/03/2006将停留,因为距离A3 = 17/02/2016还有34天。等

1 个答案:

答案 0 :(得分:1)

好的,我将在下面发布一些代码。这不是最优雅的代码,因为我没有接受VBA培训,我自己也在学习它。

我确实注意到了一个问题,它让我头撞墙几个小时。当您的日期列是格式化为以日期格式显示的序列日期时,它似乎不起作用。但是在处理日期值时它可以正常工作。当日期值由于某种原因被格式化为日期时,VLOOKUP的VBA应用程序函数无法匹配列表中的项目。

Option Explicit


Sub removedates()

Dim counter As Integer
Dim MaxDays As Integer
Dim Datelist() As Variant
Dim NumberofDates As Integer
Dim RemoveDateList As Range
Dim RemoveDate As Boolean
Dim RemoveDays As Boolean
Dim Doesitexist As Variant

MaxDays = 32

Range("A1").Select
Datelist = Range(Selection, Selection.End(xlDown))
Range("B1").Select
Set RemoveDateList = Range(Selection, Selection.End(xlDown))

NumberofDates = UBound(Datelist)
counter = 2

While counter <= NumberofDates
    Doesitexist = Application.VLookup(Datelist(counter, 1), RemoveDateList.Value, 1, False)
    RemoveDate = Not (IsError(Doesitexist))
    RemoveDays = MaxDays > Datelist(counter, 1) - Datelist(counter - 1, 1)
    If RemoveDate And RemoveDays Then
        Call Removefromlist(Datelist, counter)
        counter = counter - 1
        NumberofDates = NumberofDates - 1
    End If
    counter = counter + 1
Wend

Range(Cells(1, 3), Cells(UBound(Datelist), 3)) = Datelist

End Sub


Sub Removefromlist(arr As Variant, rowtodelete As Integer)

Dim temp As Variant
Dim i As Integer

If rowtodelete = UBound(arr) Then
    arr(rowtodelete) = ""
Else
    For i = rowtodelete To UBound(arr) - 1
        arr(i, 1) = arr(i + 1, 1)
    Next i
    arr(UBound(arr), 1) = ""
End If

End Sub

我将日期列表放入数组中,显然比处理单元格和范围更快。其中一个限制是您无法从数组中删除,所以我添加了第二个子项将列表中的所有日期移动1行,然后用“”替换最后一个条目。因此,即使新列表下方的单元格看起来是空白的,它们也不是真的。