将vba中2个日期之间的所有日期存储到数组中

发布于 2025-01-11 12:56:27 字数 939 浏览 0 评论 0原文

我正在使用一个函数来获取开始日期和结束日期列表中 2 个日期之间的所有日期:我希望将从开始日期到结束日期的每个日期及其唯一 ID 存储在数组中。数据为列 1 ID、2 开始日期、3 结束日期。该数组将是一个 ID 列表,其中包含从开始日期到结束日期的所有相关日期。下面是我必须获取所有日期的代码:

Sub Test_Dates()
'
Dim TESTWB As Workbook
Dim TESTWS As Worksheet

Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")

For i = 2 To TESTWS.Cells(1, 1).End(xlDown).Row

DatesTest = getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3))

Next i

End Sub


Function getDates(ByVal StartDate As Date, ByVal EndDate As Date) As Variant

    Dim varDates()      As Date
    Dim lngDateCounter  As Long

    ReDim varDates(0 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

I am using a function to get all dates between 2 dates in a list of Start Dates and End Dates: I am looking to store in an array each of the dates from start to end date with their unique ID. Data is column 1 ID, 2 Start Date, 3 End Date. The array would be a list of ID's with all pertaining dates from Start Date to End Date. Below is the code I have to get all dates:

Sub Test_Dates()
'
Dim TESTWB As Workbook
Dim TESTWS As Worksheet

Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")

For i = 2 To TESTWS.Cells(1, 1).End(xlDown).Row

DatesTest = getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3))

Next i

End Sub


Function getDates(ByVal StartDate As Date, ByVal EndDate As Date) As Variant

    Dim varDates()      As Date
    Dim lngDateCounter  As Long

    ReDim varDates(0 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2025-01-18 12:56:27

只需创建一个具有 ReDim DatesTest(1 To LastRow - FirstRow + 1) 行大小的数组,然后用 getDates 的结果填充该数组即可。

Dim TESTWB As Workbook
Dim TESTWS As Worksheet

Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")

Const FirstRow As Long = 2
Dim LastRow As Long
LastRow = TESTWS.Cells(1, 1).End(xlDown).Row

Dim DatesTest() As Variant
ReDim DatesTest(1 To LastRow - FirstRow + 1)

Dim i As Long
For i = FirstRow To LastRow
    DatesTest(i - FirstRow + 1) = getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3))
Next i

然后,您可以使用 DatesTest(1) 访问 getDates 的第一个结果,其中 DatesTest(1)(1) 应该为您提供第一组。

如果你想循环遍历所有这些,你可以这样做:

Dim DateSet As Variant
For Each DateSet In DatesTest  ' loop through all sets of dates
    Dim DateItem As Variant
    For Each DateItem In DateSet  ' loop through all dates of one set
        Debug.Print DateItem
    Next DateItem
Next DateSet

或者类似

Dim iSet As Long
For iSet = 1 To Ubound(DatesTest)  ' loop through all sets of dates
    Dim iDate As Long
    For iDate = 0 To Ubound(DatesTest(iSet))  ' loop through all dates of one set
        Debug.Print "Set " & iSet, "Date " & DatesTest(iSet)(iDate)
    Next iDate
Next iSet

这应该输出类似的内容

Set 1        Date 2022-03-08
Set 1        Date 2022-03-09
Set 1        Date 2022-03-10
Set 2        Date 2022-04-01
Set 2        Date 2022-03-02
Set 2        Date 2022-03-03
…

如果你想使用ID(i,1)作为键,那么你需要使用集合而不是数组。

Dim TESTWB As Workbook
Dim TESTWS As Worksheet

Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")

Const FirstRow As Long = 2
Dim LastRow As Long
LastRow = TESTWS.Cells(1, 1).End(xlDown).Row

DatesTest As New Collection

Dim i As Long
For i = FirstRow To LastRow
    DatesTest.Add getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3)), TESTWS.Cells(i, 1)
Next i

然后,您可以使用 DatesTest(TESTWS.Cells(2, 1)) 获取第一组日期和 DatesTest(TESTWS.Cells(2, 1))(1) > 会给你该组的第一个日期。

Just create an array with the size of the rows ReDim DatesTest(1 To LastRow - FirstRow + 1) and fill that with your results from getDates.

Dim TESTWB As Workbook
Dim TESTWS As Worksheet

Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")

Const FirstRow As Long = 2
Dim LastRow As Long
LastRow = TESTWS.Cells(1, 1).End(xlDown).Row

Dim DatesTest() As Variant
ReDim DatesTest(1 To LastRow - FirstRow + 1)

Dim i As Long
For i = FirstRow To LastRow
    DatesTest(i - FirstRow + 1) = getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3))
Next i

You can then access the first result of getDates with DatesTest(1) where DatesTest(1)(1) should give you the first date of the first set.

If you want to loop through all of them you can do it like that:

Dim DateSet As Variant
For Each DateSet In DatesTest  ' loop through all sets of dates
    Dim DateItem As Variant
    For Each DateItem In DateSet  ' loop through all dates of one set
        Debug.Print DateItem
    Next DateItem
Next DateSet

or like

Dim iSet As Long
For iSet = 1 To Ubound(DatesTest)  ' loop through all sets of dates
    Dim iDate As Long
    For iDate = 0 To Ubound(DatesTest(iSet))  ' loop through all dates of one set
        Debug.Print "Set " & iSet, "Date " & DatesTest(iSet)(iDate)
    Next iDate
Next iSet

This should output something like

Set 1        Date 2022-03-08
Set 1        Date 2022-03-09
Set 1        Date 2022-03-10
Set 2        Date 2022-04-01
Set 2        Date 2022-03-02
Set 2        Date 2022-03-03
…

If you want to use the ID(i,1) as key then you need to use a Collection instead of an array.

Dim TESTWB As Workbook
Dim TESTWS As Worksheet

Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")

Const FirstRow As Long = 2
Dim LastRow As Long
LastRow = TESTWS.Cells(1, 1).End(xlDown).Row

DatesTest As New Collection

Dim i As Long
For i = FirstRow To LastRow
    DatesTest.Add getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3)), TESTWS.Cells(i, 1)
Next i

Then you can use DatesTest(TESTWS.Cells(2, 1)) to get the first set of dates and DatesTest(TESTWS.Cells(2, 1))(1) would give you the first date of that set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文