PDA

View Full Version : [SOLVED:] Convert the formula to a UDF



joky
01-17-2025, 10:59 PM
Hello everyone
how can this formula be converted to a UDF?


=IF(A2<>"no";TRUNC(H2*B2;2)*E2+TRUNC(H2*B2;2)/30*D2+TRUNC(H2*C2;2)*G2+TRUNC(H2*C2;2)/30*F2;"")
Thank you in advance

p45cal
01-18-2025, 06:11 AM
It looks like you're on Excel2010 so you can't take advantage of some of the newer functions.
Your formula could be shorter:
=IF(A2<>"no";TRUNC(H2*B2;2)*(E2+D2/30)+TRUNC(H2*C2;2)*(G2+F2/30);"")
Anyway, a UDF:
Function TheCalc(condn, theVal, Pcnt1, Mnths1, Days1, Pcnt2, Mnths2, Days2)
If condn <> "no" Then
TheCalc = Evaluate("trunc(" & theVal * Pcnt1 & ",2)") * (Mnths1 + Days1 / 30) + Evaluate("trunc(" & theVal * Pcnt2 & ",2)") * (Mnths2 + Days2 / 30)
Else
TheCalc = ""
End If
End Function

On a spreadsheet:
=TheCalc(A2;H2;B2;E2;D2;C2;G2;F2)
but with 8 arguments it'd be nice to see some hints as to what belongs where as built-in functions do, but you can get close by doing the following:
In a cell type
=TheCalc(
then while the cursor is still direclty after the open parentheses character, press CTRL + Shift + A and you should then see:
=TheCalc(condn;theVal;Pcnt1;Mnths1;Days1;Pcnt2;Mnths2;Days2)
then all you have to do is replace the hints with cell references.
Note, if you don't like the name TheCalc for the function you can change it by changing all 3 instances of it to something else in the udf.

Edit: One more thing, in the udf, in the two Evaluate bits, there's ,2 which might need to be change to ;2 (replace the comma with a semicolon)

joky
01-18-2025, 11:09 AM
Very great solution. It solved my problem and I got the desired results.