Welcome to the Excel Forum !
First, let me suggest that you wrap code in tags (see my sig.) It makes reading the code much easier.
Second, analyizing the posted function
If the argument passed to the function is the string "Long" (case sensitive) then it will put the desired MAX(0, Sheet1!B2-Sheet1!B6) in Sheet1!B9.
Otherwise it will do nothing.
There are a couple of problems with that constuction.
1) Functions are for returning values. That function does not return a value, it manipulates cells, but there is no value to the function OptionType.
2) Functions that are called from a worksheet cell cannot/will not manipulate cells. The can read values from cells, but they will not write values to cells (or change cell colors or delete or ....). They only return values.
If you want the value MAX(0, Sheet1!B2-Sheet1!B6) to be put in Sheet1!B9,
You write a UDF like
and put the formula =myFunction(B2, B6) in B9.
Notice that the function does not put anything in cells, it returns a value.
The putting in B9 is done by Excel's formula evaluator, the "=" if you will.
MyFunction just give a value for the = to use.
just as it would give a value for =MyFunction(B2, B6) + 1 to use.
I don't understand where B5 and "Long" fit into your problem, but I hope this explained how UDFs work.
Bookmarks