从列中删除date

我有一个date列(列A),从最旧到最新sorting。 如果需要删除的date在B列,我不希望这个列的date距离比32天更近。

一个理解的例子。

Column A Column B "Target Column" 07/02/2006 20/01/2006 07/02/2006 11/02/2006 17/02/2006 11/02/2006 17/02/2006 17/03/2006 17/03/2006 17/03/2006 21/04/2006 21/04/2006 21/04/2006 19/05/2006 19/05/2006 

在A列从2006年2月7日开始,下一个date是11/02/2006,但它不包括在B列,所以它必须留下来。 A3是2016年2月17日,距离A2只有6天的时间,并且包含在B栏中。所以A3必须离开。 现在,A4 = 17/03/2006将会停留,因为它距A3 = 17/02/2016相隔34天。 等等

好的,我打算在下面发布一些代码。 这不是最优雅的代码,因为我没有在VBA培训,我仍然自己学习。

我注意到了一个问题,并且让我把头撞在墙上几个小时。 当您的date列是格式化以date格式显示的序列date时,它似乎不起作用。 它工作正确的时候处理date值。 当date值由于某种原因被格式化为date时,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 

我把date列表放到一个数组中,显然这比处理单元格和范围要快。 其中一个限制是你不能从数组中删除,所以我添加了第二个子将列表中的所有date向上移动1行,然后用“”replace最后一个条目。 所以即使新列表下面的单元格可能看起来是空白的,但它们并不是真的。