I'm trying to obtain the most recent date and time from a column using VBA excel.
The issue here is that once I run the macro, I obtain the correct most recent date, but the time obtained remains in a 00:00:00 value as shown in the image.
Table with dates and macro result:
How can I obtain the most recent time and date from the B column correctly?
I tried
Sub ObtainNewestDateTimeofaColumn()
Max_DateTime = Application.WorksheetFunction.Max(Columns("$B:$B"))
Max_Date = Format((CDate((Split(Max_DateTime, ".")(0)))), "dd/mm/yyyy hh:mm:ss")
MsgBox (Max_Date)
End Sub
and obtained the result : "26/02/2022 00:00:00"
The result I was expecting was: "26/02/2022 11:45:00"
The problem is in your Split
function. Dates are stored as decimal numbers with the integer part being the number of days since 31/12/1899 and the decimal part being the fraction of a day (or time).
And VBA is US-centric (decimal as dot) in many areas
When you Split
on the decimal and return the first element in the resultant array, you are effectively removing the time portion from your datetime value, hence the time is missing from your result.
Change your Max_Date=
line to read:
Max_Date = Format(max_datetime, "dd/mm/yyyy hh:mm:ss")