Need help with VBA code in datediff

relphelp

New Member
Joined
Aug 17, 2014
Messages
5
Hi Guys -

I am currently working on a small project , currently using two dates to be entered by user and wants the date difference to in "months".
However i am getting the output in "days" not able to figure out the VBA code to handle output in months.

My VBA Code -

Code:
Sub test()
Dim DateStr1 As String
Dim DateStr2 As String
Dim DateDiffStr As String
Dim Date1 As Date
Dim Date2 As Date
Dim DateDiff As Integer
Dim NumberDays As Integer


DateStr1 = InputBox("Enter First Date : ")
Date1 = DateValue(DateStr1)
DateStr2 = InputBox("Enter Second Date : ")
Date2 = DateValue(DateStr2)


temp = Date2 - Date1


NumberDays = Int(temp)


MsgBox ("Number of Days : " & temp)


End Sub
Please correct my VBA code.
Thanks in advance :)

Warm Regards
Relphelp
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,539
Office Version
365
Platform
Windows
Why don't you use DateDiff?
Code:
NumberOfMonths = DateDiff("m", Date1, Date2)
 

relphelp

New Member
Joined
Aug 17, 2014
Messages
5
Why don't you use DateDiff?
Rich (BB code):
Sub test()
Dim DateStr1 As String
Dim DateStr2 As String
Dim DateDiffStr As String
Dim Date1 As Date
Dim Date2 As Date
Dim datediff As Date
Dim numberofmonths As Integer




DateStr1 = InputBox("Enter First Date : ")
Date1 = DateValue(DateStr1)
DateStr2 = InputBox("Enter Second Date : ")
Date2 = DateValue(DateStr2)


numberofmonths = datediff("m", Date1, Date2)


MsgBox ("Number of Days : " & numberofmonths)


End Sub
If I use Code
Code:
[COLOR=#333333]NumberOfMonths = DateDiff("m", Date1, Date2)[/COLOR]
It shows me an error "expected array". Can you please help?
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,950
Office Version
365
Platform
Windows, Mobile, Web
Hello

For me, a simple test in VBA like this, works:

Code:
DateDiff("m", Date, Date + 200)
The result is 7 (months between today and 200 days from today)

Can you try again?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,539
Office Version
365
Platform
Windows
You've declared datediff as a variable.
 

Watch MrExcel Video

Forum statistics

Threads
1,095,371
Messages
5,444,064
Members
405,265
Latest member
Iram

This Week's Hot Topics

Top