在vba中获取2个日期之间的所有日期 [英] Get all dates between 2 dates in vba

查看:536
本文介绍了在vba中获取2个日期之间的所有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是vba的新手,我尝试获取2个日期之间的所有日期的vba,例如,我将使用参数01-01-2015和15-01-2015调用该函数,然后进入返回具有所有可能日期的数组,即:

I am a newbie in vba and I am trying to get in vba all dates between 2 dates, for example I will call the function with the parameters 01-01-2015 and 15-01-2015, and I will get in return an array with all the dates possibles, i.e :

01-01-2015
02-01-2015
03-01-2015
.....
15-01-2015

我没有在论坛上找到答案,所以在此先感谢您的帮助。

I didn't find the answer on the forums, so thanks in advance for your help.

推荐答案

您可以简单地将日期转换为long并进行循环(+1)并获取2个日期之间的所有日期(再次将其转换为日期)

you can simply convert the dated in long and make loop(+1) and get all dated between 2 dates(convert that to date again)

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

这篇关于在vba中获取2个日期之间的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆