First day and last day of the month values in respective textboxes

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello
How can i get 1st day of the month and last day of the month in respective textboxes
with the below code

Code:
Option Explicit
Public gDate As Date, ldate As Date, lastDay As Long

Private Sub cmdGetFirstLastDay_Click()

Dim yearNo As Integer, monthNo As Integer, dayNo As Integer

yearNo = "2018"
monthNo = "6"
dayNo = "24"

gDate = DateSerial(yearNo, monthNo, dayNo) 
End Sub

txtFirstDay.Text should display as "01-06-2018"
txtlastDay.Text should display as "30-06-2018"

Thanks
SamDsouza
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You used Function EoMonth():

Code:
Option ExplicitPublic gDate As Date, ldate As Date, lastDay As Long


Private Sub cmdGetFirstLastDay_Click()


Dim yearNo As Integer, monthNo As Integer, dayNo As Integer


yearNo = "2018"
monthNo = "6"
dayNo = "24"


gDate = DateSerial(yearNo, monthNo, dayNo) 

Debug.Print "txtFirstDay.Text : " & Format(WorksheetFunction.EoMonth(gDate, -1) + 1, "dd-mm-yyyy")
Debug.Print "txtlastDay.Text : " & Format(WorksheetFunction.EoMonth(gDate, 0), "dd-mm-yyyy")


End Sub
 
Upvote 0
You used Function EoMonth():

Code:
Option ExplicitPublic gDate As Date, ldate As Date, lastDay As Long


Private Sub cmdGetFirstLastDay_Click()


Dim yearNo As Integer, monthNo As Integer, dayNo As Integer


yearNo = "2018"
monthNo = "6"
dayNo = "24"


[B][COLOR="#FF0000"]gDate = DateSerial(yearNo, monthNo, dayNo) [/COLOR][/B]

Debug.Print "txtFirstDay.Text : " & Format(WorksheetFunction.EoMonth(gDate, -1) + 1, "dd-mm-yyyy")
Debug.Print "txtlastDay.Text : " & Format(WorksheetFunction.EoMonth(gDate, 0), "dd-mm-yyyy")


End Sub

Instead of forming the actual date from the date parts, why not form the date for the first day directly... that way the two format expressions can be somewhat simpler...

gDate = DateSerial(yearNo, monthNo, 1)

Debug.Print "txtFirstDay.Text : " & Format(gDate, "dd-mm-yyyy")
Debug.Print "txtlastDay.Text : " & Format(DateAdd("m", 1, gDate) - 1, "dd-mm-yyyy")
 
Upvote 0
Thanks sadBoy309 and Rick

Rick yours was practically good suggestion and a perfect answer. Works Great :)
gDate = DateSerial(yearNo, monthNo, 1)

Debug.Print "txtFirstDay.Text : " & Format(gDate, "dd-mm-yyyy")
Debug.Print "txtlastDay.Text : " & Format(DateAdd("m", 1, gDate) - 1, "dd-mm-yyyy")
i tried yours with purposely typing manually in txtFirst day as 5th-Jan-2019 then last day text shown as 4th-Feb-2019
But 1 question for eg even if i type 5th-jan 2019 in first day but can it show in the LastDay textbox as 31st-jan-2019 instead of 4th-Feb-2019

SamDsouza
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,852
Members
449,471
Latest member
lachbee

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