Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Calculating dates based on the current date


I have four textboxes on a form for a user to enter four dates. The four
dates a
1.) Beginning date of the current period.
2.) Ending date of the current period.
3.) Beginning date of the previous period.
4.) Ending date of the previous period.

I would like for the default text of the first textbox to be the first
day of the previous month and the second textbox to be the last day of
the previous month.
The third textbox will have the first day of the previous month and the
year will be the previous year. The fourth textbox will have the last
day of the previous month and the year will be the previous year.

IE: If todays date is 3/29/2006 then the first textbox will have
2/1/2006, the second will have 2/28/2006, the third will have 2/1/2005,
and the fourth will have 2/28/2005. (Im ignoring leap years for now)

This will all be done with VBA, probably in Private Sub
UserForm_Initialize()

I am able to get the previous month with this code:
StartDate = DateAdd("m", -1, HoldDate) HoldDate = current month

I also know how to get the last day of the previous month with:
iDaysInMonth = Day(DateAdd("d", -1, DateSerial _
(Year(StartDate), Month(StartDate) + 1, 1)))

iDaysInMonth will now be 28.

What would be the easiest way to take that information and get
variables that hold the dates 2/1/2006, 2/28/2006, 2/1/2005, and
2/28/2005.

Ive found that some of the code I would have used with VB.Net is not
working with VBA in Excel. Otherwise, I would have it solved.

Are there any suggestions on the best way to approach this using VBA?

Thanks


--
DavidW
------------------------------------------------------------------------
DavidW's Profile: http://www.excelforum.com/member.php...o&userid=32630
View this thread: http://www.excelforum.com/showthread...hreadid=527350

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Calculating dates based on the current date

I think you can make it a little more straightforward with the DateSerial
function.
First day of prior month:
=DateSerial(Year(Date),Month(Date)-1,1)
Last day of prior month:
=DateSerial(Year(Date),Month(Date),1)-1
For last year:
=DateSerial(Year(Date)-1,Month(Date)-1,1)
=DateSerial(Year(Date)-1,Month(Date),1)-1
--
- K Dales


"DavidW" wrote:


I have four textboxes on a form for a user to enter four dates. The four
dates a
1.) Beginning date of the current period.
2.) Ending date of the current period.
3.) Beginning date of the previous period.
4.) Ending date of the previous period.

I would like for the default text of the first textbox to be the first
day of the previous month and the second textbox to be the last day of
the previous month.
The third textbox will have the first day of the previous month and the
year will be the previous year. The fourth textbox will have the last
day of the previous month and the year will be the previous year.

IE: If todays date is 3/29/2006 then the first textbox will have
2/1/2006, the second will have 2/28/2006, the third will have 2/1/2005,
and the fourth will have 2/28/2005. (Im ignoring leap years for now)

This will all be done with VBA, probably in Private Sub
UserForm_Initialize()

I am able to get the previous month with this code:
StartDate = DateAdd("m", -1, HoldDate) HoldDate = current month

I also know how to get the last day of the previous month with:
iDaysInMonth = Day(DateAdd("d", -1, DateSerial _
(Year(StartDate), Month(StartDate) + 1, 1)))

iDaysInMonth will now be 28.

What would be the easiest way to take that information and get
variables that hold the dates 2/1/2006, 2/28/2006, 2/1/2005, and
2/28/2005.

Ive found that some of the code I would have used with VB.Net is not
working with VBA in Excel. Otherwise, I would have it solved.

Are there any suggestions on the best way to approach this using VBA?

Thanks


--
DavidW
------------------------------------------------------------------------
DavidW's Profile: http://www.excelforum.com/member.php...o&userid=32630
View this thread: http://www.excelforum.com/showthread...hreadid=527350


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating a percenatge based on dates Oldmanoutofhisdepth Excel Worksheet Functions 1 November 5th 09 02:46 PM
calculating differentials based on dates MBOF10 Excel Worksheet Functions 0 May 12th 08 03:09 PM
Calculating days between current date and a date in future NETWORKDAYS() function Faheem Khan Excel Worksheet Functions 2 February 10th 05 07:18 PM
Calculating current age from 2 date cells dhealy Excel Worksheet Functions 1 January 19th 05 04:50 PM
Calculating Due Dates Based on Payments Eric Hanson Excel Worksheet Functions 2 January 6th 05 02:41 AM


All times are GMT +1. The time now is 07:41 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright 2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"