Finding min and max from an array of dates

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Not sure what I'm doing wrong here... I have a 1D array of dates and I want to use the Application.Worksheetfuntion.Max/Min to find the earliest and latest dates?

Thought this would be straight forward - any thoughts of what I'm doing wrong (I'm getting 12:00:00AM for both variables)? (see attached pic)
 

Attachments

  • Screen Shot 04-14-23 at 09.04 AM.JPG
    Screen Shot 04-14-23 at 09.04 AM.JPG
    164.7 KB · Views: 26

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA Code:
Dim vDateArray As Variant
vDateArray = Range("A1:A13").Value2
Try to cast Value2 property.
 
Upvote 0
Hello.
How have you defined (Dim) the variable 'GetFirstLastDaiInArray'?...
 
Upvote 0
Here's the function:

VBA Code:
Public Function GetFirstLastDateInArray(vArray As Variant, sMode As String) As Date

Dim i As Long, j As Long
Dim vDateArray As Variant
Dim dDate As Date

ReDim vDateArray(1 To UBound(vArray, 1) - 1)

For i = LBound(vArray, 1) + 1 To UBound(vArray, 1)
    j = j + 1
    vDateArray(j) = CDate(vArray(i, 1))
Next i

If sMode = "First" Then
    dDate = Application.WorksheetFunction.Min(vDateArray)
ElseIf sMode = "Last" Then
    dDate = Application.WorksheetFunction.Max(vDateArray)
End If

GetFirstLastDateInArray = dDate

End Function
 
Upvote 0
Try
VBA Code:
Public Function GetFirstLastDateInArray(vArray As Range, sMode As String) As Date

If sMode = "First" Then
    GetFirstLastDateInArray = Application.WorksheetFunction.Min(vArray)
ElseIf sMode = "Last" Then
    GetFirstLastDateInArray = Application.WorksheetFunction.Max(vArray)
End If

End Function
 
Upvote 0
vArray is a 2D array that I get from a csv file (wrote a function to do that). The dates are in the 3rd column.

VBA Code:
If Not IsArrayAllocated(vDeliverables_) Then
        Application.ScreenUpdating = False
        vDeliverables_ = GetCSVInArray("DeliverablesDB.csv")
        Application.ScreenUpdating = True
End If

vArray = Application.Index(vDeliverables_, 0, 3)
mdStartDate = GetFirstLastDateInArray(vArray, "First")
mdEndDate = GetFirstLastDateInArray(vArray, "Last")
 
Upvote 0
Oh you are using a csv file.. So the dates are not actual dates.. Hmm.. That's why you are casting to date. How about casting the date to a long and then convert it back?
VBA Code:
Public Function GetFirstLastDateInArray(vArray As Variant, sMode As String) As Date

Dim i As Long, j As Long
Dim vDateArray As Variant
Dim dDate As Date

ReDim vDateArray(1 To UBound(vArray, 1) - 1)

For i = LBound(vArray, 1) + 1 To UBound(vArray, 1)
    j = j + 1
    vDateArray(j) = CLng(CDate(vArray(i, 1)))
Next i
With Application
If sMode = "First" Then
    dDate = CDate(.WorksheetFunction.Min(vDateArray))
ElseIf sMode = "Last" Then
    dDate = CDate(.WorksheetFunction.Max(vDateArray))
End If
End With
GetFirstLastDateInArray = dDate

End Function
 
Upvote 0
Solution
Flashbond,

That did it! But I'm confused... the original function produced an array of dates (e.g. #2022-04-13#). Should not the min/max functions operate on these values?
 
Upvote 0
Yeah, that's a strange behaviour. I have no idea why it is not working in vba. Although it's working fine with ranges. Maybe it's a problem with arrays. I don't know :/
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,207
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top