Search code examples
excelvbaapipostudf

Run UDF after others functions?


I have a UDF that collects results of other functions and sends them to an web API which calculates and sends a response. e.g.

           A                      B                   C               D
7  =CalcResult(E7, F7)    =CalcResult(G7, H7)        234     =SendToAPI(A7,B7,C7)
8  =CalcResult(E8, F8)    =CalcResult(G8, H8)        274     =SendToAPI(A8,B8,C8)
9         ...                    ...                 ...             ...

What happens is that when you Run the SendtoAPI it Posts to the API twice one with 2 nulls and the value in c then the second with the correct data after the calcResult is finished.

Is there any way of ordering which gets called first?

Set the SendToAPI to run last?


Solution

  • UDFs are often calculated more than once in the calculation sequence when they are called with not-yet-calculated parameters. An un-calculated variant parameter appears to the UDF as an empty variant: so the solution is to exit the UDF without sending to the web api if any of the parameters are empty.

    See my website page http://www.decisionmodels.com/calcsecretsj.htm for more details

    And my blog post https://fastexcel.wordpress.com/2011/11/25/writing-efficient-vba-udfs-part-7-udfs-calculated-multiple-times/