VBA/Excelで二項演算を行っています。私はgetFact()と呼ばれる独自の階乗関数を構築しました。私のコードを実行しようとするたびに、エラーなしで、エラーが発生します。私はそれをデバッグしようとしており、コードは最初のオプション "選択1:正確"では動作しますが、メニューの他のオプションでは機能しません:少なくとも、ほとんど以上、より大きい...などVBA/Excelその他二項計算でエラーが発生していない場合
ここは私のコードです:
Private Sub Binomial_click()
Dim n As Integer
Dim p As Double
Dim q As Integer
Dim r As Integer
Dim i As Integer
Dim s As Integer
Dim Selection As Integer
Dim results As Double
Dim fromo As Double
Dim too As Double
'start of proc
MsgBox ("this will calculate the binomial")
Selection = InputBox("1. Exact" & vbNewLine _
& "2. At Least" & vbNewLine _
& "3. At Most" & vbNewLine _
& "4. Greater than" & vbNewLine _
& "5. Less than" & vbNewLine _
& "6. Between")
'parameters needed
n = InputBox("input number of trials")
p = InputBox("input probabilty")
q = 1 - p
If Selection = 1 Then 'exact
r = InputBox("input success")
results = getFact(n)/(getFact(n - r) * getFact(r)) * p^r * q^(n - r)
MsgBox ("your result is" & results)
ElseIf Selection = 2 Then 'at least
r = InputBox("input success") 'asks for successes
For i = r To n
results = results + getFact(n)/getFact(n - i) * getFact(i) * p^i * q^
(n - i)
ElseIf Selection = 3 Then 'at most
r = InputBox("input success") 'asks for successes
For i = 0 To r
results = results + getFact(n)/(getFact(n - i) * getFact(i)) * p^i * q^
(n - i)
ElseIf Selection = 4 Then 'greater than
r = InputBox("input success") 'asks for successes
s = r + 1
For i = s To n
results = results + getFact(n)/(getFact(n - i) * getFact(i)) * p^i * q^
(n - i)
ElseIf Selection = 5 Then 'less than
r = InputBox("input success") 'asks for successes
s = r - 1
For i = 0 To s
results = results + getFact(n)/(getFact(n - i) * getFact(i)) * p^i * q^
(n - i)
ElseIf Selection = 6 Then 'between
fromo = InputBox("input from")
too = InputBox("input to")
For i = fromo To too
results = results + getFact(n)/(getFact(n - i) * getFact(i)) * p^i * q^
(n - i)
MsgBox ("your results are" & results)
End If
End Sub
は、あなたがあなたのFor
ループのそれぞれを終了するNext
声明、例えば不足している、事前に
あなたがダウンロードしてください:[Rubberduck VBA](http://rubberduckvba.com/)。 Code Formatterを使用すると、このような問題をきちんと捕まえることができます。 –