Help with CVErr Error Trapping in a function

Joseph Lo

New Member
Joined
Dec 19, 2013
Messages
40
Hi all,

I am having a lot of trouble to trap the #Value in a function call.

Here is a sample code that I tried and it is not working. Could anyone please be kind enough to take a look?

if item1 and item2 are decimal values, the function will return the correct value.
if item1 or item2 is a string, the function will return the #value which is what I wanted to trap....and I don't know how.

Thanks


Code:
Function CCB(item1 As Double, item2 As Double) As Variant


Dim test As Variant
test = item1 * item2


If IsError(test) Then


    Select Case test
    Case CVErr(xlErrValue) '#Value Error
        CCB = "Error in input"
    End Select
Else
    CCB = item1 * item2
End If




End Function
 
I use your code and the function CCB still return a #VALUE error if either item1 and item2 is a string....if item1 and item2 is a numerical value, then the function works but not otherwise.

What should I amend?

Isn't that the exact behavior you want?

What you describe above is exactly the behavior you would get from the Sum function for example. So what you have is a UDF that conforms exactly to standard worksheet function behavior, no?

And @Boswell has already fixed the problem raised by @jstola. That was to change to Variant Type for item1 and item2...
 
Last edited:
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Thanks Boswell. That code does exactly what I need to do.

Thanks also to everyone who chip in to help me. Appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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