Reputation: 315
Currently working on a function, and as part of it i need to find the oldest date in a column in VBA. Have spent a while looking for solutions but cant find much. I found one line however it doesn't actually work. Ill post the code snippet I have but any suggestions or solutions would be greatly appreciated!
Public Function SetSOWDates() As String
Dim wsCRC As Worksheet
Set wsCRC = Worksheets("CRC")
Dim LastRowInCRC As Long
LastRowInCRC = CRC.LastRowInCRC()
LatestDate = Format(wsCRC.Max(Range("K8:K" & LastRowInCRC)), "dd/mm/yyyy")
Debug.Print LatestDate
End Function
Upvotes: 4
Views: 9192
Reputation:
You are mixing up worksheetfunction calls with the worksheet and trying to stuff text-that-looks-like-a-date into a long integer.
Public Function SetSOWDates() As String
Dim wsCRC As Worksheet
Dim LastRowInCRC As Long
Set wsCRC = Worksheets("CRC")
LastRowInCRC = wsCRC.LastRowInCRC()
LatestDate = application.Min(wsCRC.Range("K8:K" & LastRowInCRC))
Debug.Print Format(LatestDate, "dd/mm/yyyy")
End Function
There was also a typo in CRC.LastRowInCRC()
instead of wsCRC.LastRowInCRC()
.
Upvotes: 0
Reputation: 34045
You want Min
, not Max
for the oldest date, and it's a member of WorksheetFunction
rather than the Worksheet
:
LatestDate = Format(Worksheetfunction.Min(wsCRC.Range("K8:K" & LastRowInCRC)), "dd/mm/yyyy")
Upvotes: 5