VBA error

Aurora118

New Member
Joined
Oct 10, 2007
Messages
42
Hi,

I am trying to run a macro and I keep getting an error message.
Can anyone pinpoint where the mistake is please?

Function MonthDiff(date1 As Date, date2 As Date) As Long
MonthDiff = DateDiff(m, date1, date2)
End Function

Sub Calc_Dates_Stat2()

Dim prev_date As Date
Dim curr_date As Date
Dim Months
Dim NA As Boolean

Range("U2").Select

Do Until IsEmpty(Range("T" & ActiveCell.Row))

NA = False

If IsEmpty(Range("B" & ActiveCell.Row)) Then
NA = True
Else
curr_date = Range("B" & ActiveCell.Row).Value
End If

If IsEmpty(Range("A" & ActiveCell.Row)) Then
NA = True
Else
prev_date = Range("A" & ActiveCell.Row).Value
End If

If NA = False Then
ActiveCell.Value = MonthDiff(prev_date, curr_date)
End If

ActiveCell.Offset(1, 0).Select

Loop

End Sub


Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
What is the error description and what line does it break on? I.e. the ine should be highlighted in yellow.

Perhaps instead of using the IsEmpty() function you should be using IsDate().
 
Upvote 0
MonthDiff = DateDiff(m, date1, date2)
End Function

This is the part where is falls over.

Run time error 5
Invalid procedure call or argument
 
Upvote 0
Try :-
Rich (BB code):
Function MonthDiff(date1 As Date, date2 As Date) As Long
MonthDiff = DateDiff("m", date1, date2)
End Function
Mick
 
Upvote 0
m should be in speech marks, i.e. "m"

Although all that function does is avoids you having to input the first argument, so you might as well just call DateDiff in your main routine.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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