** Originally posted within worksheet formulas subcategory, but I later discovered that most of the threads in that subcategory deal with built-in Excel formulas. I am therefore re-posting here.

Hello,
I'm fairly new to VBA programming, and very new to creating custom functions. The function I'm trying to create would apply the TDIST worksheet function to the result of a series of calculations. Four arguments would be needed to feed the series of calculations.

The code I have been using below results in the #VALUE! error.

As far as I can tell, the formulas for the interim calculations are correct, although there could very well be errors in my code that attempts to make those calculations.

Any help would be appreciated. Thanks in advance.



Function INDPROPS(pct1 As Double, pct2 As Double, _
                  base1 As Double, base2 As Double) As Double
    Dim diff        As Double
    Dim pbar        As Double
    Dim sediffs     As Double
    Dim zstat       As Double

    diff = Abs(pct1 - pct2)
    pbar = (pct1 * base1 + pct2 * base2) / (base1 + base2)
    sediffs = Sqr(pbar * (1 - pbar)) * (1 / base1 + 1 / base2)
    zstat = Abs((diff - 0.5 * (1 / base1 + 1 / base2)) / sediffs)

    INDPROPS = 0.995 - WorksheetFunction.TDist(zstat, 100000000, 2)
End Function