Consulting

Results 1 to 3 of 3

Thread: Convert the formula to a UDF

  1. #1

    Convert the formula to a UDF

    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
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,959
    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)
    Last edited by p45cal; 01-18-2025 at 06:25 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Very great solution. It solved my problem and I got the desired results.
    Last edited by Aussiebear; 04-14-2025 at 04:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •