2017-11-26 4 views
0

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声明、例えば不足している、事前に

+1

あなたがダウンロードしてください:[Rubberduck VBA](http://rubberduckvba.com/)。 Code Formatterを使用すると、このような問題をきちんと捕まえることができます。 –

答えて

1

をありがとう

For i = 0 To s 
    results = results + getFact(n)/(getFact(n - i) * getFact(i)) * p^i * q^(n - i) 
Next 

リファクタリング、コードは(計算ループの複数のコピーを避けるために)次のようになります。

Sub test() 
    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 


    Select Case Selection 
    If Selection = 1 Then 'exact 
     r = InputBox("input success") 
     fromo = r 
     too = r 

    ElseIf Selection = 2 Then 'at least 
     r = InputBox("input success") 'asks for successes 
     fromo = r 
     too = n 

    ElseIf Selection = 3 Then 'at most 
     r = InputBox("input success") 'asks for successes 
     fromo = 0 
     too = r 

    ElseIf Selection = 4 Then 'greater than 
     r = InputBox("input success") 'asks for successes 
     fromo = r + 1 
     too = n 

    ElseIf Selection = 5 Then 'less than 
     r = InputBox("input success") 'asks for successes 
     fromo = 0 
     too = r - 1 

    ElseIf Selection = 6 Then 'between 
     fromo = InputBox("input from") 
     too = InputBox("input to") 

    Else 
     MsgBox "Invalid selection" 
     Exit Sub 

    End If 

    For i = fromo To too 
     results = results + getFact(n)/(getFact(n - i) * getFact(i)) * p^i * q^(n - i) 
    Next 
    MsgBox "your results are" & results 

End Sub 
+2

いい仕事ですが、それは "Select Caseのために悲鳴を上げる" ...笑です。 +1 –

+0

@ThomasInzina私は実際に 'Select Case'を入れ始めました。そしてOPが使っていた' If'/'ElseIf'に戻ってきました。 (私はトレーニングに一度のアプローチで一歩進んだことにしました:D) – YowE3K

+0

最初のユースケースのニースケース。数式が同じで、一度だけ実行する必要があることは明らかではありません。 –

関連する問題