VBA to find most recent date

Tanyaann1995

Board Regular
Joined
Mar 24, 2021
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a code below that finds the most recent date in a list and displays it.

VBA Code:

Sub dateshow()

Dim max As Date
max = Application.WorksheetFunction.max(Columns("A"))
MsgBox CDate(max)

End Sub

My list of dates is below. But, when i try the above code, the message box displays 12:00:00 AM. Please advise why.

16-Jul-2020
23-Jul-2020
23-Jul-2020
23-Jul-2020
08-Dec-2020
07-Jan-2021
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,​
maybe your dates are not Date data type … To avoid a NES the better is to link your workbook on a files host website …​
 
Upvote 0
Hi,​
maybe your dates are not Date data type … To avoid a NES the better is to link your workbook on a files host website …​
Hi,

I didn't understand your reply. Pls explain further. Im not able to figure this out.
 
Upvote 0
Assuming that your data starts in A2, in a blank column in row 2 put the formula
Excel Formula:
=ISNUMBER(A2)
and drag down.
Are all the results TRUE?
 
Upvote 0
You could try this which shouldnt mind if the dates are textual.

VBA Code:
Sub dateshow()

Dim arr, myMax As Date, lr As Long

lr = Range("A" & Rows.Count).End(xlUp).Row
arr = Range("A1:A" & lr)

For i = LBound(arr) To UBound(arr)
    If IsDate(arr(i, 1)) Then
        If arr(i, 1) > myMax Then
            myMax = arr(i, 1)
        End If
    End If
Next

MsgBox CDate(myMax)

End Sub
 
Upvote 0
Solution
You could try this which shouldnt mind if the dates are textual.

VBA Code:
Sub dateshow()

Dim arr, myMax As Date, lr As Long

lr = Range("A" & Rows.Count).End(xlUp).Row
arr = Range("A1:A" & lr)

For i = LBound(arr) To UBound(arr)
    If IsDate(arr(i, 1)) Then
        If arr(i, 1) > myMax Then
            myMax = arr(i, 1)
        End If
    End If
Next

MsgBox CDate(myMax)

End Sub
Thank you. this worked :)
 
Upvote 0

Forum statistics

Threads
1,214,999
Messages
6,122,645
Members
449,093
Latest member
Ahmad123098

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