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
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Why don't you use DateDiff?
Code:
NumberOfMonths = DateDiff("m", Date1, Date2)
 
Upvote 0
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?
 
Upvote 0
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?
 
Upvote 0
You've declared datediff as a variable.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top