如何获取date – 1年的VBA

我有一排交易date,存储在A列。它包含交易日,因此它不包含一年中的所有日子。 我想得到一个特定的date,并从中删除一年。 我想在同一列中find新的单元格的索引。 如果不可能,我想find下一个最接近的date。

我到目前为止所尝试的:

Dim date1 As Double date1 = Sheets("Part2").Cells(i, 1).Value Dim matchRow As Integer matchRow = 3 While Sheets("1.A").Cells(matchRow, 1).Value <> date1 matchRow = matchRow + 1 Wend 

我可以在工作表中得到一个特定的date,但是现在我需要得到一年前的date,如果不是下一个最近的date。

需要一些指导来做到这一点。

还尝试过:表格(“Part2”)。单元格(i,1)。值-365。 它不工作..

使用DateAdd :

 DateAdd("yyyy",-1,Sheets("Part2").Cells(i, 1).Value) 

UPD自从你的date以格式“yyyymmdd”存储为文本,从第3行开始,使用这个:

 Dim date1 As Date Dim srtDate1 As String, srtDate2 As String Dim matchRow strDate1 = Sheets("Part2").Cells(i, 1).Value date1 = DateSerial(Left(strDate1, 4), Mid(strDate1, 3, 2), Right(strDate1, 2)) srtDate2 = Format(DateAdd("yyyy", -1, date1), "yyyymmdd") matchRow = Application.Match(CDbl(srtDate2), Sheets("Part2").Range("A:A"), 1) If IsError(matchRow) Then matchRow = 3 Else matchRow = matchRow + 1 End If MsgBox "new date: " & Sheets("Part2").Range("A" & matchRow)