#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Date Query

I need to display a date in a field, this needs to default to the
first of the nearest month, 60 days from todays date.

So in other words for today it should return 01/12/07 (DD/MM/YY).

How would this be done using Excel VBA please?

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Date Query

Is nearest month for Jan 16th Jan or Feb?

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"David" wrote in message
ups.com...
I need to display a date in a field, this needs to default to the
first of the nearest month, 60 days from todays date.

So in other words for today it should return 01/12/07 (DD/MM/YY).

How would this be done using Excel VBA please?

Thanks!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Date Query

Here is a start:

Note: I am in the US so these are US format.

dt = dateSerial(year(date),Month(date),day(date)+60)
? dateSerial(year(dt),month(dt),1)
11/01/2007


you can get the first day of the next month with

dt = dateSerial(year(date),Month(date),day(date)+60)
? dateSerial(year(dt),month(dt)+1,1)
12/01/2007

Beyond that, it depends on what you mean by nearest.

format(dateSerial(year(dt),month(dt)+1,1),"dd/mm/yy")
will give you the format you describe.

--
Regards,
Tom Ogilvy


"David" wrote:

I need to display a date in a field, this needs to default to the
first of the nearest month, 60 days from todays date.

So in other words for today it should return 01/12/07 (DD/MM/YY).

How would this be done using Excel VBA please?

Thanks!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Date Query

Thanks all, it wasn't as hard as I thought at first, this works:

' Prepopulate the data for the first of the month, plus 60 days
hence.
Today = Date
' Now add 60 days
NextDate = DateAdd("d", 60, Today)
Dayte = Day(NextDate)

If (Dayte = 1) Then
' do nothing, it's the right day
dFinalDate = dDayte
Else
' Need to find the 1st of the following month
Dayte = Dayte - 1
dFinalDate = DateAdd("m", 1, NextDate)
dFinalDate = DateAdd("d", -Dayte, dFinalDate)
End If

Range("E8").Value = dFinalDate

By all means comment if you see any problems or can make it more
efficient, thanks again!

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
date query Hilton Excel Discussion (Misc queries) 2 November 20th 07 10:42 AM
Date query Crimson King Excel Worksheet Functions 4 October 24th 07 01:54 AM
Date Query Treza Blu Excel Discussion (Misc queries) 4 May 31st 07 09:11 PM
How do I enter a date range ie -7 from current date in MS QUERY notsmartenough Excel Discussion (Misc queries) 1 November 11th 05 10:17 PM
Date query stevieh Excel Programming 0 June 3rd 04 10:36 AM


All times are GMT +1. The time now is 11:24 PM.

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"