IsNumeric / IsError returns an error

I'm new to VBA and working on a custom function (which will be used for more complex purposes in the future) - that's why I want to do this using VBA instead of standard formulas) whose task is to get the date from a cell and indicate its position (row number ) in a column (for example, column A).

This is a simple task that I solved with the following procedure:

Function TempRowNumber(OrgDate)
    TempRowNumber = WorksheetFunction.Match(OrgDate, Range("A:A"), 0)
End Function

      

This works well, of course. However, the problem is that it can happen that the entered function is not included in the range. In this case, the function should search for the last entered date before that date. Since dates are always sorted in chronological order in this range, I planned to build an error checking formula (as a condition) and a corresponding loop.

The problem is if I write a formula like this:

=ISERROR(MATCH(C23;A:A;0))

      

all this is good (the formula returns TRUE if the date from cell C23 is in column A and FALSE if the date is not found). But if you try to rewrite this formula into a VBA procedure, ia:

Function FindDate(OrgDate)

FindDate = IsError(WorksheetFunction.Match(OrgDate, Range("A:A"), 0))

End Function

      

the user-defined function returns an error #VALUE!

if the date is not found (and FALSE if it is missing).
Below I am attaching a snapshot of what I am describing.

Do you know what to do? Thanks in advance.

+3


source to share


1 answer


I don't know the reason for this, but this works:

Function FindDate(Orgdate)
    FindDate = IsError(Application.Match(Orgdate, Range("A:A"), 0))
End Function

      



Logically it should work the same way WorksheetFunction.Match

and can be evaluated using the IsError

same, but I tried it and I get the same result as yours.

+2


source







All Articles