在VBA中从date范围获取特定date

我有一个在列A中有一个date范围的工作表。我已经find了最后一行的方式:

LastRow = Worksheets("TIME").Cells(Rows.Count, "A").End(xlUp).Row 

现在我正试图获得具体的date。 该范围不包含周末,但由于date是专有的,我不能使用WorkDayfunctionfind我所需要的。

情况1:

从最后一个可用date开始,我试图获得一年前的date(如果date不可用,请select下一个可用date)。

我在这里做的是使用datefunction和减去1年..

 day1Y = date(year(LastRow)-1,month(LastRow),day(LastRow)) 

为了匹配,我将date范围转换为数组,并使用函数确定它是否在数组中。 如果是,得到它,但如果不是,我不知道如何得到下一个可用。

 Dim DateArray() as Variant Dim WantedDate1 as date Dim WantedDate2 as date DateArray() = Worksheets("TIME").Range("A2:A" & LastRow).Value If IsInArray(day1Y) = True then WantedDate1 = .Cells(1,LastRow).Value End if 

案例2:

从最后一个可用date开始,我试图在同一年内获得第一个date(如果最后一个date是2015年8月10日,则根据该date范围内的date获取2015年的第一个可用date)。

 WantedDate2 = Year(.Cells(1,LastRow).Value) 

我得到了最后一天的那一年,但是再次找不到那一年的第一次约会。

任何帮助将深表谢意。

使用一个循环来增加1乘1的天数,并testing它是否是在旅途中的数组:

 Option Explicit Sub DGMS89() Dim wsT As Worksheet Dim LastRow As Double Dim DateArray() As Variant Dim LastDate As Date Dim Day1y As Date Dim WantedDate1 As Date Dim WantedDate2 As Date Set wsT = ThisWorkbook.Sheets("TIME") LastRow = wsT.Cells(wsT.Rows.Count, "A").End(xlUp).Row DateArray() = wsT.Range("A2:A" & LastRow).Value LastDate = DateArray(UBound(DateArray, 1)) Day1y = DateAdd("yyyy", -1, LastDate) WantedDate1 = Day1y If IsInArray(WantedDate1) Then Else Do While Not IsInArray(WantedDate1) WantedDate1 = DateAdd("d", 1, WantedDate1) Loop End If WantedDate2 = DateSerial(year(LastDate), 1, 1) Do While Not IsInArray(WantedDate2) WantedDate2 = DateAdd("d", 1, WantedDate2) Loop End Sub