Good day.!

How to get the last day of the current month and store it in a variable.?


Thank you for giving time.?

Here are a couple of ways and by no means are these the only ways...

Option Explicit

Private Sub Form_Load()
Dim M As Integer, D As Integer, Y As Integer
Dim S As String, LastDay As Date

M = Month(Now)
D = Day(Now)
Y = Year(Now)

If IsDate(M & "/" & 28 & "/" & Y) = True Then D = 28
If IsDate(M & "/" & 29 & "/" & Y) = True Then D = 29
If IsDate(M & "/" & 30 & "/" & Y) = True Then D = 30
If IsDate(M & "/" & 41 & "/" & Y) = True Then D = 31

LastDay = M & "/" & D & "/" & Y
S = "The last day of this month is " & Format(LastDay, "dddd") & " "

MsgBox S & LastDay

'or
LastDay = M & "/" & 1 & "/" & Y
LastDay = DateAdd("m", 1, LastDay)
LastDay = DateAdd("d", -1, LastDay)

S = "The last day of this month is " & Format(LastDay, "dddd") & " "
MsgBox S & LastDay
End Sub

Good Luck

Here are a couple of ways and by no means are these the only ways...

Option Explicit

Private Sub Form_Load()
Dim M As Integer, D As Integer, Y As Integer
Dim S As String, LastDay As Date

M = Month(Now)
D = Day(Now)
Y = Year(Now)

If IsDate(M & "/" & 28 & "/" & Y) = True Then D = 28
If IsDate(M & "/" & 29 & "/" & Y) = True Then D = 29
If IsDate(M & "/" & 30 & "/" & Y) = True Then D = 30
If IsDate(M & "/" & 41 & "/" & Y) = True Then D = 31

LastDay = M & "/" & D & "/" & Y
S = "The last day of this month is " & Format(LastDay, "dddd") & " "

MsgBox S & LastDay

'or
LastDay = M & "/" & 1 & "/" & Y
LastDay = DateAdd("m", 1, LastDay)
LastDay = DateAdd("d", -1, LastDay)

S = "The last day of this month is " & Format(LastDay, "dddd") & " "
MsgBox S & LastDay
End Sub

Good Luck

Thank you for giving time with this problem. Anyway ive got i very simpler solution with this problem.the code below. This works very well.

Private Sub Form_Load()
Dim lastdayofmonth

lastdayformonth = CDate(DateAdd("d", -1, DateAdd("m", 1, Month(Now()) & "/1/" & Year(Now()))))

MsgBox lastdayformonth
End Sub

Thank you guys.This problem was solved.!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.