2016-04-08 5 views
1

Laufen ist dies eine einfache Änderung Code Aktienkurs meine Code-Funktion (mit Parametern) isteine Vba-Funktion auf Knopfdruck funktioniert nicht in Excell

Function VanillaCall(S0 As Single, Exercise As Single, Mean As Single, sigma As Single, _ 
Interest As Single, Time As Single, Divisions As Integer, Runs As Integer) As Single 

deltat = Time/Divisions 
interestdelta = Exp(Interest * deltat) 
up = Exp(Mean * deltat + sigma * Sqr(deltat)) 
down = Exp(Mean * deltat - sigma * Sqr(deltat)) 
pathlength = Int(Time/deltat) 

piup = (interestdelta - down)/(up - down) 
pidown = 1 - piup 
Temp = 0 

For Index = 1 To Runs 
    upcounter = 0 
    For j = 1 To pathlength 
     If Rnd > pidown Then upcounter = upcounter + 1 
    Next j 
     callvalue = Application.Max(S0 * (up^upcounter) * (down^(pathlength - upcounter)) - Exercise, 0)/(interestdelta^pathlength) 
     Temp = Temp + callvalue 
Next Index 

VanillaCall = Temp/Runs 

End Function 

Parameter von Zellen in Excel übergeben werden. Ich möchte diese Funktion aus Knopf klicken und Rückgabewert in einer Zelle anzeigen sagen b12. Ich habe versucht, den Code in eine Schaltfläche sub, aber es funktioniert nicht, ein Aufruf vancylacall innerhalb sub nicht funktioniert nicht. wie ..

private sub button1_click() 
call vanillacall 
end sub 
+0

Wenn Sie in button1_click() die Funktion vanillacall aufrufen, müssen Sie alle notwendigen Argumente übergeben. – Mrig

+0

Ich glaube nicht, dass das funktionieren wird, weil Sie '= VanillaCall()' in eine Zelle setzen und die notwendigen Argumente übergeben müssen. Sie können die Argumente übergeben, indem Sie diese Funktion aus einem Makro aufrufen, aber dann müssen Sie definieren, wo Sie das Ergebnis in diesem Makro selbst ausgeben möchten. – newguy

+0

aber meine Argumente kommen von Zellen in Excel, die auf der laufenden Funktion – user2997767

Antwort

1
Private Sub button1_click() 
    Range("B12").Value = vanillacall(....) 
End Sub 

Wie pro Ihre Anfrage, Übergeben von Argumenten im Bereich wie unten. Im Folgenden Code nur zum Beispiel ist (aufgrund der Änderungen in Excel-Daten)

Sub testing33() 
    Range("B12") = sample(Range("A5"), Range("B5")) 
End Sub 

Function sample(a As Range, b As Range) 
    sample = a.Cells.Value & ", " & b.Cells.Value 
End Function 
+0

aber ich bin nicht übergeben Argumente dort im mit Excell Zelle Daten als Argumente, die ausgewählt sind, wenn die Funktion – user2997767

+0

verwendet, Excel-Zelle Daten als Argument –

+0

in Ordnung, habe es running.Thanks , aber was passiert, wenn Excel Datenwerte ändert sich. – user2997767

0

Sie müssen die Werte aus dem Blatt zu bekommen und in Variablen speichern. Übergeben Sie dann die Variablen an die Funktion. Dann gib das Ergebnis irgendwo auf das Blatt aus. Sie müssen die Bereichsadressen und den Arbeitsblattnamen entsprechend anpassen.

Private sub button1_click() 

    dim ws as worksheet 
    Set ws = worksheets("Sheet1") ' < change the sheet name as appropriate 

    dim S0 As Single 
    dim Exercise As Single 
    dim Mean As Single 
    dim sigma As Single 
    dim Interest As Single 
    dim Time As Single 
    dim Divisions As Integer 
    dim Runs As Integer As Single 

    S0 = ws.Range("B1") '< specify the cell that has this data 
    Exercise = ws.Range("B2") '< specify the cell that has this data 
    Mean = ws.Range("B3") '< specify the cell that has this data 
    sigma = ws.Range("B4") '< specify the cell that has this data 
    Interest = ws.Range("B5") '< specify the cell that has this data 
    Time = ws.Range("B6") '< specify the cell that has this data 
    Divisions = ws.Range("B7") '< specify the cell that has this data 
    Runs = ws.Range("B8") '< specify the cell that has this data 

    dim Result as Single 
    Result = vanillacall(S0, Exercise , Mean, sigma, Interest, Time, Divisions, Runs) 
    ws.Range("B10") = Result '<specify the cell where you want the result 

end sub 
1

Ich würde so etwas wie die, unter dem tun würde mir erlauben, den Bereich wählen Sie die Daten Ich möchte enthält, an die Funktion übergeben (solange der Bereich angrenzt und enthält 8 Zellen) und die Zelle holen Ich möchte das Ergebnis nach ausgeben.

Private Sub button1_click() 
Dim inRng As Range, outRng As Range 

inSelect: 
Set inRng = Application.InputBox("Select Range to Calculate", Type:=8) 
    If inRng.Cells.Count <> 8 Then 
     MsgBox "Select a range with 8 cells!", vbCritical 
     GoTo inSelect 
    End If 
outSelect: 
Set outRng = Application.InputBox("Select Cell to Output To", Type:=8) 
    If outRng.Cells.Count > 1 Then 
     MsgBox "Select only one cell!", vbCritical 
     GoTo outSelect 
    End If 

outRng.Value = VanillaCall(inRng.Cells(1), inRng.Cells(2), inRng.Cells(3), inRng.Cells(4), inRng.Cells(5), inRng.Cells(6), inRng.Cells(7), inRng.Cells(8)) 

End Sub