Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default The difference between two dates.

I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.

Any help would be much appreciated.

Thanks,

Peter

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default The difference between two dates.

Just subtract them and format the cell as General.

Peter wrote:

I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.

Any help would be much appreciated.

Thanks,

Peter


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default The difference between two dates.

MsgBox #1/22/2007# - #1/20/2007#
MsgBox DateDiff("d", #1/20/2007#, #1/22/2007#)

Either would do - half the fun is getting the date into a format readable by
VBA and/or Excel. DateDiff has other periods you can enter. Other useful
functions include DateValue and DateSerial. Help's pretty good on these.

"Peter" wrote:

I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.

Any help would be much appreciated.

Thanks,

Peter


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default The difference between two dates.

A1: 20/01/2007
A2: 22/01/2007

msgbox Range("A2").Value2 - Range("A1").Value2

this assumes your regional settings are in dd/mm/yyyy

--
Regards,
Tom Ogilvy


"Peter" wrote:

I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.

Any help would be much appreciated.

Thanks,

Peter


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default The difference between two dates.

On 1 Feb, 16:06, Tom Ogilvy
wrote:
A1: 20/01/2007
A2: 22/01/2007

msgbox Range("A2").Value2 - Range("A1").Value2

this assumes your regional settings are in dd/mm/yyyy

--
Regards,
Tom Ogilvy



"Peter" wrote:
I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.


Any help would be much appreciated.


Thanks,


Peter- Hide quoted text -


- Show quoted text -


All,

Thanks for your help.

This is based on Smallweed's code:

Sub DateDiff()

a = "02/1/2007"
b = "20/1/2007"

MsgBox DateDiff("d", a, b)

End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default The difference between two dates.

I assume you saying that doesn't work as I wouldn't expect it to.

first, you named your procedure datediff which causes an error. Name it
something else.

This adjustment should work:

Sub DateCheck()

a = "2/1/2007"
b = "20/1/2007"

MsgBox DateDiff("d", CDate(a), CDate(b))

End Sub

--
Regards,
Tom Ogilvy


"Peter" wrote:

On 1 Feb, 16:06, Tom Ogilvy
wrote:
A1: 20/01/2007
A2: 22/01/2007

msgbox Range("A2").Value2 - Range("A1").Value2

this assumes your regional settings are in dd/mm/yyyy

--
Regards,
Tom Ogilvy



"Peter" wrote:
I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.


Any help would be much appreciated.


Thanks,


Peter- Hide quoted text -


- Show quoted text -


All,

Thanks for your help.

This is based on Smallweed's code:

Sub DateDiff()

a = "02/1/2007"
b = "20/1/2007"

MsgBox DateDiff("d", a, b)

End Sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default The difference between two dates.

On 1 Feb, 17:31, Tom Ogilvy
wrote:
I assume you saying that doesn't work as I wouldn't expect it to.

first, you named your procedure datediff which causes an error. Name it
something else.

This adjustment should work:

Sub DateCheck()

a = "2/1/2007"
b = "20/1/2007"

MsgBox DateDiff("d", CDate(a), CDate(b))

End Sub

--
Regards,
Tom Ogilvy

"Peter" wrote:
On 1 Feb, 16:06, Tom Ogilvy
wrote:
A1: 20/01/2007
A2: 22/01/2007


msgbox Range("A2").Value2 - Range("A1").Value2


this assumes your regional settings are in dd/mm/yyyy


--
Regards,
Tom Ogilvy


"Peter" wrote:
I am trying get the macro to tell me the difference between two dates.
ie, the difference between 20/01/2007 and 22/01/2007 is two days.


Any help would be much appreciated.


Thanks,


Peter- Hide quoted text -


- Show quoted text -


All,


Thanks for your help.


This is based on Smallweed's code:


Sub DateDiff()


a = "02/1/2007"
b = "20/1/2007"


MsgBox DateDiff("d", a, b)


End Sub


Tom, thank you for pointing out the error with the title. The code
works with this exception.

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
Difference between two dates Sheeloo Excel Discussion (Misc queries) 0 March 3rd 10 05:25 PM
Difference in dates Charles Eaves[_2_] New Users to Excel 4 May 11th 09 11:35 AM
difference of 2 dates sierrabunny Excel Worksheet Functions 1 May 10th 09 09:08 AM
Difference of two dates Zafar Excel Worksheet Functions 3 July 26th 07 08:44 AM
difference between two dates dhouston1000 Excel Discussion (Misc queries) 2 August 24th 05 11:19 AM


All times are GMT +1. The time now is 08:48 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"