+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    12-21-2009
    Location
    Cincinnati, OH
    MS-Off Ver
    Excel 2007
    Posts
    4

    Custom function problem

    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.

    Code:
    Function INDPROPS(pct1, pct2, base1, base2)
    
    
    Dim diff As Variant
    Dim pbar As Variant
    Dim sediffs As Variant
    Dim zstat As Variant
    Dim ABSZSTAT As Variant
    
    diff = Abs(pct1 - pct2)
    pbar = (pct1 * base1 + pct2 * base2) / (base1 + base2)
    sediffs = Application.WorksheetFunction.Sqrt(pbar * (1 - pbar)) * (1 / base1 + 1 / base2)
    zstat = (diff - 0.5 * (1 / base1 + 1 / base2)) / sediffs
    ABSZSTAT = Abs(zstat)
    
       INDPROPS = 0.995 - Application.WorksheetFunction.TDist(ABSZSTAT, 100000000, 2)
    
    End Function

  2. #2
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,134

    Re: Custom function problem

    100,000,000 sounds like a lot of degrees of freedom. Have you tried doing the same thing with worksheet functions?

    Sqr is a native function in VBA.

    Code:
    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
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    12-21-2009
    Location
    Cincinnati, OH
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Custom function problem

    Yes, the 100000000 degrees of freedom works fine in the worksheet function; it's basically just saying that we are sampling from a very large population (the formula is computing statistical testing diagnostics in a survey research setting).

    Thanks for the note about Sqr. I think I had it that way originally, but as part of my (unsuccessful) troubleshooting, I tried calling the worksheet function. That doesn't appear to be the problem.

  4. #4
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,134

    Re: Custom function problem

    So step through the code, check the value of the inputs when it breaks, and then try the same values from the worksheet.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0