Hello,
I'm very new to VBA and so I think I have a question that should be easy to answer. What I'm trying to do is create a custom function that performs just like vlookup, except if the vlookup returns an error I would like it to show zero. Effectively, the function would mimic this formula: IF(ISERROR(VLOOKUP(...)),0,(VLOOKUP(...)).
I want to create this custom function b/c I find myself typing the above function I'd say once a week (for about a year now). So, I'm trying to create my own custom function.
Below I've tried to create my custom vlookup function, but it still acts just like the normal vlookup - if there is an error it displays an error instead of zero.
Again, I'm new to VBA so my code below is probably full of novice errors. Any help would be greatly appreciated,
Justin
Public Function ErrorVlookup(zlookupvalue, zrange, zColumnIndex, zLogic) As Single
Dim zVar As String
zVar = WorksheetFunction.vlookup(zlookupvalue, zrange, zColumnIndex, zLogic)
On Error GoTo ErrorHandler
ErrorVlookup = zVar
Exit Function
ErrorHandler:
ErrorVlookup = 0
End Function
I'm very new to VBA and so I think I have a question that should be easy to answer. What I'm trying to do is create a custom function that performs just like vlookup, except if the vlookup returns an error I would like it to show zero. Effectively, the function would mimic this formula: IF(ISERROR(VLOOKUP(...)),0,(VLOOKUP(...)).
I want to create this custom function b/c I find myself typing the above function I'd say once a week (for about a year now). So, I'm trying to create my own custom function.
Below I've tried to create my custom vlookup function, but it still acts just like the normal vlookup - if there is an error it displays an error instead of zero.
Again, I'm new to VBA so my code below is probably full of novice errors. Any help would be greatly appreciated,
Justin
Public Function ErrorVlookup(zlookupvalue, zrange, zColumnIndex, zLogic) As Single
Dim zVar As String
zVar = WorksheetFunction.vlookup(zlookupvalue, zrange, zColumnIndex, zLogic)
On Error GoTo ErrorHandler
ErrorVlookup = zVar
Exit Function
ErrorHandler:
ErrorVlookup = 0
End Function