How to generate #VALUE! error as output for a LAMBDA function?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

Is there a way to generate #VALUE! error for the output of a LAMBDA function?

For built-in functions, if the wrong values are entered for the parameters, the functions return the #VALUE! error, and I want to mimic that behavior for my LAMBDAs. (Essentially, I need something like the NA() function that generates the #N/A! error.)

Thanks for any input! 🤗
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Something like this?
Excel Formula:
=LAMBDA(val,IF(val=1,"OK",NA()))(2)

I don't think there is a function to generate #VALUE!, but I think we can trick Excel by simply generating a #VALUE! error.
Excel Formula:
=LAMBDA(val,IF(val=1,"OK",VALUE("")))(2)
 
Upvote 0
Solution
Yes, thanks, that's exactly what I was looking for; simple and compact.
 
Upvote 0
Something like this?
Excel Formula:
=LAMBDA(val,IF(val=1,"OK",NA()))(2)

I don't think there is a function to generate #VALUE!, but I think we can trick Excel by simply generating a #VALUE! error.
Excel Formula:
=LAMBDA(val,IF(val=1,"OK",VALUE("")))(2)
What about giving it a function call that will generate a #VALUE! error, for example...

=LAMBDA(val,IF(val=1,"OK",LN("A")))(2)
 
Upvote 0
Yes, thanks, that's exactly what I was looking for; simple and compact.
Glad to hear it helps.

What about giving it a function call that will generate an #VALUE! error, for example...

=LAMBDA(val,IF(val=1,"OK",SUM("A")))(2)
Exactly. I also had the same intention with VALUE("") function since the VALUE function will return #VALUE! error with blank parameter. I just wanted to indicate VALUE indicated in the formula somehow. SUM will work as well.
 
Upvote 0
OK, I just cooked this up:

Excel Formula:
=LAMBDA(error_type,CHOOSE(error_type,#NULL!,#DIV/0!,#VALUE!,#REF!,#NAME?,#NUM!,#N/A,,,,,,,,,,,,#EXTERNAL!))

It works with errors 1 through 7 and 19, as listed above. However, 8 didn't come out as expected (so I removed it), and 9 through 14 were not possible to include in the formula as it was giving me a function error upon hitting ENTER.

Any idea how to get this to work with all error types? Will be a great LAMBDA to post in the LAMBDA section.
 
Upvote 0
If it is ok to return the error type as string for the ones after #9 then you can use text instead (only shown for #SPILL! below).
(#8 works for me by the way)

Excel Formula:
=LAMBDA(error_type,CHOOSE(error_type+1,#EXTERNAL!,#NULL!,#DIV/0!,#VALUE!,#REF!,#NAME?,#NUM!,#N/A,#GETTING_DATA,"#SPILL!"))(8)
Note: #EXTERNAL! is actually 0, so we need to make CHOOSE to work with zero, that's why I used error_type + 1 for the index number.
 
Upvote 0
If it is ok to return the error type as string for the ones after #9 then you can use text instead
Thanks! The text versions aren't as cool, but for now I'll live with that until I or anyone else finds a way 😅

#8 works for me by the way
Yeah, I was also able to hit ENTER with 8, but the output was only text and the little error triangle at the top left didn't appear. But perhaps this particular error doesn't have a triangle? 😅

#EXTERNAL! is actually 0
I see, thanks for letting me know. I was going based off the argument list that appears when you type =ERROR.TYPE( which shows external to be 19. So is it only 0 or both 0 and 19?
 
Upvote 0
I see, thanks for letting me know. I was going based off the argument list that appears when you type =ERROR.TYPE( which shows external to be 19. So is it only 0 or both 0 and 19?
Yes, the constant index is 19 but for some reason ERROR.TYPE(#EXTERNAL!) returns 0 (mysterious Excel Development Team!), and this is supposed to be the constant value. So, if there is an #EXTERNAL! error/notification in a cell, then its values should be 0, at least according to ERROR.TYPE.
 
Upvote 0
OK, I managed to add two more errors to the function, i.e. #FIELD! and #CALC!. I added a "not supported" message for the four errors that currently don't work with the function, i.e. #SPILL!, #CONNECT!, #BLOCKED!, and #UNKNOWN!:

Excel Formula:
=LAMBDA(error_type,
   LET(e,error_type+1,
      IF(OR(e<1,e>15,MOD(e,1)<>0),
         "Invalid error number!",
         IF(ISERROR(MATCH(e,{1,2,3,4,5,6,7,8,9,14,15},0)),
            "Not supported!",
            CHOOSE(e,#EXTERNAL!,#NULL!,#DIV/0!,#VALUE!,#REF!,#NAME?,#NUM!,#N/A,#GETTING_DATA,,,,,INDIRECT("").a,LAMBDA(""))
         )
      )
   )
)

Cell Formulas
RangeFormula
C2C2=LAMBDA(error_type,LET(e,error_type+1,IF(OR(e<1,e>15,MOD(e,1)<>0),"Invalid error number!",IF(ISERROR(MATCH(e,{1,2,3,4,5,6,7,8,9,14,15},0)),"Not supported!",CHOOSE(e,#EXTERNAL!,#NULL!,#DIV/0!,#VALUE!,Sheet1!#REF!,#NAME?,#NUM!,#N/A,#GETTING_DATA,,,,,INDIRECT("").a,LAMBDA(""))))))
C4C4=XERROR(0)
D4:D22D4=TYPE(C4)
C5C5=XERROR(1)
C6C6=XERROR(2)
C7C7=XERROR(3)
C8C8=XERROR(4)
C9C9=XERROR(5)
C10C10=XERROR(6)
C11C11=XERROR(7)
C12C12=XERROR(8)
C13C13=XERROR(9)
C14C14=XERROR(10)
C15C15=XERROR(11)
C16C16=XERROR(12)
C17C17=XERROR(13)
C18C18=XERROR(14)
C19C19=XERROR(-1)
C20C20=XERROR(2.2)
C21C21=XERROR(17)
C22C22=XERROR(17.3)


At this point the function covers all of the most common errors, so I'm gonna go ahead and post this in the LAMBDA section, and if I or anyone figures out tricks for the remaining four errors, we can update it over time.
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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