UDF no looping and returning answer for worksheet function

SerenityNetworks

Board Regular
Joined
Aug 13, 2009
Messages
131
Office Version
  1. 365
Platform
  1. Windows
I'm trying to return the answer to the formula for BINOMDIST to the worksheet. However, unless I comment out the "cdf" row, the macro does not loop. I don't know what I'm doing wrong here. Any help will be appreciated.

Thanks,
Andrew

Code:
Function AllowedErrors(Size As Double, Conf As Double, p As Double) As Long
  Dim cdf           As Double   ' cumulative distribution function
  Dim nSize         As Double   ' calculation counter
  Dim nErrors       As Double   ' counter for errors
  If Conf <= 0# Or Conf >= 1# Then Exit Function
  If p <= 0# Or p >= 1# Then Exit Function
  If nErrors = 0& Then nErrors = 1 ' sets initial value of nErrors to 1
  With WorksheetFunction
        Do
          cdf = .BinomDist(nErrors, Conf, p, True)
          Debug.Print (nErrors) & " | " & (Conf) & " | " & (p) & " | " & (cdf)
          nErrors = nErrors + 1&
        Loop While nErrors < Size + 1
  End With
End Function

A
B
C
1
Accuracy
75.00%
2
Confidence
50.00%
3
4
Allowed Errors
Sample Size
5
3
A5: =AllowedErrors(B5, $B$2, 1-$B$1)
6
7
A6: =AllowedErrors(B6, $B$2, 1-$B$1)
7
11
A7: =AllowedErrors(B7, $B$2, 1-$B$1)
8
15
A8: =AllowedErrors(B8, $B$2, 1-$B$1)

<tbody>
</tbody>
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Ugh! Never mind for the moment. I realize I'm populating the binomdist function incorrectly. (I also forgot to add "AllowedErrors = cdf" after the End With.

I need to populate the binomdist function correctly and then stop the loop when the required accuracy is met.

I'm not too good at this, so if someone wants to help me out then I won't turn down the assistance. Otherwise, I'll work on it.

Thanks,
Andrew
 
Upvote 0
Okay, I was able to get it working. However, I'm not a coder. If someone would care to check the code to see if it could use some improvement then I'd be appreciative.

Thanks,
Andrew

Code:
Function AllowedErrors(Size As Double, Conf As Double, p As Double) As Long
  Dim cdf           As Double   ' cumulative distribution function
  Dim nErrors       As Double   ' counter for errors
  Conf = 1 - Conf   ' converts required confidence to form of value binomdist function outputs
  p = 1 - p         ' converts required accuracy to "probability" value binomdist function is expecting
  If Conf <= 0# Or Conf >= 1# Then Exit Function
  If p <= 0# Or p >= 1# Then Exit Function
  If nErrors = 0& Then nErrors = 1 ' sets initial value of nErrors to 1
  With WorksheetFunction
        Do Until cdf > Conf
          'cdf = .BinomDist(Allowed Errors, Sample Size, Accuracy (p), True)
          cdf = WorksheetFunction.BinomDist(nErrors, Size, p, True)
          prevErrors = nErrors
          nErrors = nErrors + 1
          'Debug.Print Round(cdf, 4) & " | " & prevErrors
         Loop
  End With
  AllowedErrors = nErrors - 2
  'Debug.Print "=binomdist(" & AllowedErrors & "," & Size & "," & p & ",TRUE)"
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,896
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top