×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Calculate the First date of the previous month & Last date of previous month.2

## Calculate the First date of the previous month & Last date of previous month.

(OP)
Hello I have a form which has a textbox for a start date: txtDateStart and an end date: txtDateEnd

I have been able to set the default values as follows:

txtDateStart = DateSerial(Year([Date]),Month([Date])-1,1)

txtDateEnd = DateSerial(Year(Date()),Month(Date())+1,0)

This works fine in that it gives me the start and end dates of the CURRENT month.

Can someone help me tweak the code so I am able to set it to display the start and end dates of the PREVIOUS month.

Many thanks Mark

### RE: Calculate the First date of the previous month & Last date of previous month.

Hi,

Previous month..

#### CODE

txtDateStart = DateSerial(Year([Date]),Month([Date])-1-1,1)

txtDateEnd = DateSerial(Year(Date()),Month(Date())+1-1,0) 

I just produced the code for current month and subtracted 1 from the month parameter of DateSerial.

Skip,

Just traded in my OLD subtlety...
for a NUance!

### RE: Calculate the First date of the previous month & Last date of previous month.

"This works fine in that it gives me the start and end dates of the CURRENT month." - not really.

Your code gives you the Start date of the Previous month (December) and the End of the Current month (January):

#### CODE

Debug.Print "Start " & DateSerial(Year(Date), Month(Date) - 1, 1)
Start 12/1/2017

Debug.Print "End   " & DateSerial(Year(Date), Month(Date) + 1, 0)
End   1/31/2018 

So you are either half wrong, or half way to your solution....

---- Andy

There is a great need for a sarcasm font.

### RE: Calculate the First date of the previous month & Last date of previous month.

Andy, I du
I didn't think to check the validity of the start and end.

Whatever current month formula, just subtract 1 from the month parameter in DateSerial.

#### CODE

CurStart: DateSerial(Year(Date), Month(Date), 1)
CurEnd: DateSerial(Year(Date), Month(Date) +1, 0)

PrvStart: DateSerial(Year(Date), Month(Date)-1, 1)
PrvEnd: DateSerial(Year(Date), Month(Date) +1-1, 0) 

Skip,

Just traded in my OLD subtlety...
for a NUance!

### RE: Calculate the First date of the previous month & Last date of previous month.

(OP)
Back on track. Thank you very much. Mark ðŸ¤—

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!