When the user enters too many arguments for the COUNTBLANK function,the function displays this error message, and returns to edit mode:
You've entered too many arguments for this function.
How to make any UDF work like that?
For example:
Function COUNT2 (c As Range)
COUNT2 = c.Count
End Function
By default this UDF returns #VALUE! error when the user enters more than one argument.
How to make it work like the COUNTBLANK function?
We can only simulate the behavior to some extent, but it will never be exactly similar to the behavior of the built-in functions of Excel.
We can add a mechanism so that when the UDF detects some syntax error, it initiates some error data (msg to display, cell to activate) that the workbook will manipulate once calculation is over. Of course this should be displayed only once, not for each cell holding an error formula, because it would be extremely stressing for the user.
The UDF checks the syntax and sets appropriate error info:
To test this, try entering wrong formulas like
=COUNT2(A1:A20, B1:B20)or=COUNT2(12345), the error message will show and the cell will be again in edit mode, unless you pressESCape. When there are many erroneous cells (copy the error cell and paste in many others), then you pressF9, the message will appear once only and one of those cells will be in edit mode.