I've got function in VBA that operates with dates from cells. But I got By ref error
when I choose cells. What can be the problem? This is my code
Function DifferenceInYears(existdate As Date, estimdate As Date) As Double
Dim yearDifference As Integer
Dim monthDifference As Integer
Dim dayDifference As Integer
yearDifference = 0
monthDifference = 0
dayDifference = 0
If (estimdate <= existdate) Then
MsgBox "Input correct range"
GoTo myerr
End If
Dim tempDate As Date
IsDateLeapDay = False
Dim existYear As String
Dim estimYear As String
existYear = Year(existdate)
estimYear = Year(estimdate)
estimMonth = Month(estimdate)
existMonth = Month(existdate)
and so on...
Just guessing here, but whatever function is calling this code, is NOT passing in an actual VB
Date
, and since you haven't qualified your arguments with theByVal
, VB assumes it to beByRef
. (Prefixing nothing—which is what you've done—is the same as prefixing withByRef
.) E.g.,is the same as
But much different than
You can try to make the arguments
ByVal
. Or you can change the argument type to Variant (which is what a Cell's value actually is, and what I'm guessing you're actually passing in).Try changing your method signature to this:
...or this:
You'll probably have to do some casting inside or outside the function, but that should get you closer and solve your
By ref error
issue.This should get you started: