我是 vba 的新手,我试图在 vba 中获取 2 个日期之间的所有日期,例如,我将使用参数 01-01-2015 和 15-01-2015 调用该函数,然后我将返回一个数组使用所有可能的日期,即:
01-01-2015
02-01-2015
03-01-2015
.....
15-01-2015
Run Code Online (Sandbox Code Playgroud)
我在论坛上没有找到答案,所以在此先感谢您的帮助。
您可以简单地将日期转换为 long 并进行循环(+1)并获取 2 个日期之间的所有日期(再次将其转换为日期)
Sub Calling()
Dim test
test = getDates(#1/25/2015#, #2/5/2015#)
End Sub
Function getDates(ByVal StartDate As Date, ByVal EndDate As Date) As Variant
Dim varDates() As Date
Dim lngDateCounter As Long
ReDim varDates(1 To CLng(EndDate) - CLng(StartDate))
For lngDateCounter = LBound(varDates) To UBound(varDates)
varDates(lngDateCounter) = CDate(StartDate)
StartDate = CDate(CDbl(StartDate) + 1)
Next lngDateCounter
getDates = varDates
ClearMemory:
If IsArray(varDates) Then Erase varDates
lngDateCounter = Empty
End Function
Run Code Online (Sandbox Code Playgroud)