Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JCO JCO is offline
external usenet poster
 
Posts: 54
Default Macro for Dates

I have a report that requires the current date in the right header and the
date range in left header. The date range is the first day of the previous
month to the last day of the previous month.

Example: Today is 11/05/2013. As I run the macro, it should look like
below:
Date Range: 10/01/2013 - 10/31/2013 Date:
11/05/2013

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Macro for Dates

On Tuesday, November 5, 2013 6:02:11 PM UTC-6, JCO wrote:
I have a report that requires the current date in the right header and the

date range in left header. The date range is the first day of the previous

month to the last day of the previous month.



Example: Today is 11/05/2013. As I run the macro, it should look like

below:

Date Range: 10/01/2013 - 10/31/2013 Date:

11/05/2013



Thanks


Well, first you get today's date (11/5/2013) with the TODAY() function as the formula in your rightmost cell. Then set a Date variable to this cell value and take advantage of the fact that you can do date arithmetic. Subtract the number of days in the previous month (31 for Oct) and the number of days in the current date less one (-31-5+1) to get the start of the previous month. Then add the number of days in the previous month less one (+31-1) to get the ending date. Use the Format function to format the starting and ending dates the way you want them to look. Only tricky thing here is you have to handle leap years to determine whether 28 or 29 days in February.

Denis
  #3   Report Post  
Posted to microsoft.public.excel.programming
JCO JCO is offline
external usenet poster
 
Posts: 54
Default Macro for Dates

Thanks, I did figure out the code yesterday. I'm sure I used more code than
required but it does work (including for leap year.

"Denis" wrote in message
...
On Tuesday, November 5, 2013 6:02:11 PM UTC-6, JCO wrote:
I have a report that requires the current date in the right header and
the

date range in left header. The date range is the first day of the
previous

month to the last day of the previous month.



Example: Today is 11/05/2013. As I run the macro, it should look like

below:

Date Range: 10/01/2013 - 10/31/2013 Date:

11/05/2013



Thanks


Well, first you get today's date (11/5/2013) with the TODAY() function as
the formula in your rightmost cell. Then set a Date variable to this cell
value and take advantage of the fact that you can do date arithmetic.
Subtract the number of days in the previous month (31 for Oct) and the
number of days in the current date less one (-31-5+1) to get the start of
the previous month. Then add the number of days in the previous month
less one (+31-1) to get the ending date. Use the Format function to
format the starting and ending dates the way you want them to look. Only
tricky thing here is you have to handle leap years to determine whether 28
or 29 days in February.

Denis


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Macro for Dates

hi JCO,

Dim firstDay As Date, lastDay As Date

firstDay = DateSerial(Year(Now), Month(Now) - 1, 1)
lastDay = DateSerial(Year(Now), Month(Now) - 1,
Day(DateSerial(Year(Now), Month(Now), 1 - 1)))

With ActiveSheet.PageSetup
.LeftHeader = "Date Range: " & firstDay & " - " & lastDay
End With

isabelle

Le 2013-11-05 19:02, JCO a écrit :
I have a report that requires the current date in the right header and
the date range in left header. The date range is the first day of the
previous month to the last day of the previous month.

Example: Today is 11/05/2013. As I run the macro, it should look like
below:
Date Range: 10/01/2013 - 10/31/2013 Date:
11/05/2013

Thanks

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Macro for Dates

to adjust date format in different language

Dim FirstDay As Date, LastDay As Date
Dim sFirstDay As String, sLastDay As String

FirstDay = DateSerial(Year(Now), Month(Now) - 1, 1)
LastDay = DateSerial(Year(Now), Month(Now) - 1,
Day(DateSerial(Year(Now), Month(Now), 1 - 1)))

sFirstDay = Format(FirstDay, "mm/dd/yyyy")
sLastDay = Format(LastDay, "mm/dd/yyyy")

With ActiveSheet.PageSetup
.LeftHeader = "Date Range: " & sFirstDay & " - " & sLastDay
End With

isabelle

Le 2013-11-06 18:26, isabelle a écrit :
hi JCO,

Dim firstDay As Date, lastDay As Date

firstDay = DateSerial(Year(Now), Month(Now) - 1, 1)
lastDay = DateSerial(Year(Now), Month(Now) - 1,
Day(DateSerial(Year(Now), Month(Now), 1 - 1)))

With ActiveSheet.PageSetup
.LeftHeader = "Date Range: " & firstDay & " - " & lastDay
End With

isabelle

Le 2013-11-05 19:02, JCO a écrit :
I have a report that requires the current date in the right header and
the date range in left header. The date range is the first day of the
previous month to the last day of the previous month.

Example: Today is 11/05/2013. As I run the macro, it should look like
below:
Date Range: 10/01/2013 - 10/31/2013 Date:
11/05/2013

Thanks



  #6   Report Post  
Posted to microsoft.public.excel.programming
JCO JCO is offline
external usenet poster
 
Posts: 54
Default Macro for Dates

Thanks,
Your version is much less code than I used. I was not familiar with the
"DateSerial".
I will review it more later

"isabelle" wrote in message ..
to adjust date format in different language

Dim FirstDay As Date, LastDay As Date
Dim sFirstDay As String, sLastDay As String

FirstDay = DateSerial(Year(Now), Month(Now) - 1, 1)
LastDay = DateSerial(Year(Now), Month(Now) - 1, Day(DateSerial(Year(Now),
Month(Now), 1 - 1)))

sFirstDay = Format(FirstDay, "mm/dd/yyyy")
sLastDay = Format(LastDay, "mm/dd/yyyy")

With ActiveSheet.PageSetup
.LeftHeader = "Date Range: " & sFirstDay & " - " & sLastDay
End With

isabelle

Le 2013-11-06 18:26, isabelle a écrit :
hi JCO,

Dim firstDay As Date, lastDay As Date

firstDay = DateSerial(Year(Now), Month(Now) - 1, 1)
lastDay = DateSerial(Year(Now), Month(Now) - 1,
Day(DateSerial(Year(Now), Month(Now), 1 - 1)))

With ActiveSheet.PageSetup
.LeftHeader = "Date Range: " & firstDay & " - " & lastDay
End With

isabelle

Le 2013-11-05 19:02, JCO a écrit :
I have a report that requires the current date in the right header and
the date range in left header. The date range is the first day of the
previous month to the last day of the previous month.

Example: Today is 11/05/2013. As I run the macro, it should look like
below:
Date Range: 10/01/2013 - 10/31/2013 Date:
11/05/2013

Thanks


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
Macro to Check Dates Beep Beep Excel Programming 1 March 15th 10 05:31 PM
Macro or formula for dates cmatera Excel Worksheet Functions 1 September 4th 08 07:44 PM
Macro to identify bad dates Sandeman[_13_] Excel Programming 3 March 23rd 06 02:34 PM
too many dates macro nastech Excel Discussion (Misc queries) 2 January 21st 06 08:37 PM
Macro changes the format of dates Rodolfo Silva Excel Programming 4 August 29th 03 05:12 AM


All times are GMT +1. The time now is 10:17 AM.

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

About Us

"It's about Microsoft Excel"