I Keep getting a #value error in Excel VBA - vba

So I wrote a quick function in VBA for Excel, but every time I call it, it gives me a #value error. I don't know what I am doing wrong. Can anyone help?
Function h(UA, k, A, Af_At, Delta, l)
h1 = 0
m = (2 * h1 / k / Delta) ^ 0.5
ml = m * l
Nf = WorksheetFunction.Tanh(ml)
No = 1 - Af_At * (1 - Nf / ml)
UA1 = h1 * A * No / 2
While UA > UA1
UA_old = UA1
h_old = h1
h1 = h1 + 0.5
m = (2 * h1 / k / Delta) ^ 0.5
ml = m * l
Nf = WorksheetFunction.Tanh(ml)
No = 1 - Af_At * (1 - Nf / ml)
UA1 = h1 * A * No / 2
Wend
h = h_old + (UA - UA_old) * (h1 - h_old) / (UA1 - UA_old)
End Function
I call it using: =h(10,1,1,1,1,1) in the insert function bar.

Division by zero at
No = 1 - Af_At * (1 - Nf / ml)
m1 is zero because h1 is zero.
You should change:
h1 = 0

Related

Block If without End If during the barrier option calculation

I am not able to compute the Barrier Option, because it shows me an error in the first line (where I wrote function). The code is as following. Thank you in advance.
Function UOBarrierOption(S As Double, q As Double, T As Double, X As Double, r As Double, _
sigma As Double, CallPutFlag As String, H As Double, K As Double, phi As Double, eta As Double)
Dim x1 As Double, x2 As Double
Dim y1 As Double, y2 As Double
Dim z As Double, mu As Double, lambda As Double
Dim AA As Double, BB As Double, CC As Double, DD As Double, EE As Double, FF As Double
mu = (r - q - sigma ^ 2 / 2) / (sigma ^ 2)
lambda = Sqr(mu ^ 2 + 2 * r / sigma ^ 2)
x1 = Log(S / X) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
x2 = Log(S / H) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
y1 = (Log(H ^ 2) / S / S) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
y2 = (Log(H / S)) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
z = Log(H / S) / (sigma * Sqr(T)) + lambda * sigma * Sqr(T)
AA = phi * S * Exp(-q * T) * Application.NormSDist(phi * x1) - phi * X * Exp(-r * T) * Application.NormSDist(phi * x1 - phi * sigma * Sqr(T))
BB = phi * S * Exp(-q * T) * Application.NormSDist(phi * x2) - phi * X * Exp(-r * T) * Application.NormSDist(phi * x2 - phi * sigma * Sqr(T))
CC = phi * S * Exp(-q * T) * (H / S) ^ (2 * (mu + 1)) * Application.NormSDist(eta * y1) - phi * X * Exp(-r * T) * (H / S) ^ (2 * mu) * Application.NormSDist(eta * y1 - eta * sigma * Sqr(T))
DD = phi * S * Exp(-q * T) * (H / S) ^ (2 * (mu + 1)) * Application.NormSDist(eta * y2) - phi * X * Exp(-r * T) * (H / S) ^ (2 * mu) * Application.NormSDist(eta * y2 - eta * sigma * Sqr(T))
EE = K * Exp(-r * T) * (Application.NormSDist(eta * x2 - eta * sigma * Sqr(T)) - (H / S) ^ (2 * mu) * Application.NormSDist(eta * y2 - eta * sigma * Sqr(T)))
FF = K * Exp(-r * T) * (Application.NormSDist(-eta * x2 + eta * sigma * Sqr(T)) + (H / S) ^ (2 * mu) * Application.NormSDist(eta * y2 - eta * sigma * Sqr(T)))
If CallPutFlag = "Cdi" Then
If X > H Then
UOBarrierOption = CC + EE
ElseIf X < H Then
UOBarrierOption = AA - BB + DD + EE
End Function
ElseIf CallPutFlag = "Cui" Then
If X > H Then
UOBarrierOption = AA + EE
ElseIf X < H Then
UOBarrierOption = BB - CC + DD + EE
End Function
ElseIf CallPutFlag = "Pdi" Then
If X > H Then
UOBarrierOption = BB - CC + DD + EE
ElseIf X < H Then
UOBarrierOption = AA + EE
End Function
ElseIf CallPutFlag = "Pui" Then
If X > H Then
UOBarrierOption = AA - BB + DD + EE
ElseIf X < H Then
UOBarrierOption = CC + EE
End Function
ElseIf CallPutFlag = "Cdo" Then
If X > H Then
UOBarrierOption = AA - CC + FF
ElseIf X < H Then
UOBarrierOption = BB - DD + FF
End Function
ElseIf CallPutFlag = "Cuo" Then
If X > H Then
UOBarrierOption = F
ElseIf X < H Then
UOBarrierOption = AA - BB + CC - DD + FF
End Function
ElseIf CallPutFlag = "Pdo" Then
If X > H Then
UOBarrierOption = AA - BB + CC - DD + FF
ElseIf X < H Then
UOBarrierOption = F
End Function
ElseIf CallPutFlag = "Puo" Then
If X > H Then
UOBarrierOption = BB - DD + FF
ElseIf X < H Then
UOBarrierOption = AA - CC + FF
End Function
End If
End Function
P.S. I have different "phi"s and "eta"s for different types of option barriers (cdi, pdi and etc.). Right now I am trying different combinations, but it also gives "end if function missing" type of error
If your function returns something you must declare the type returned in the function, and assign the returned value to the function, for exmaple:
Function CalculateSquareRoot(NumberArg As Double) As Double
If NumberArg < 0 Then ' Evaluate argument.
Exit Function ' Exit to calling procedure.
Else
CalculateSquareRoot = Sqr(NumberArg) ' Return square root.
End If
End Function
See the As Double and the CalculateSquareRoot = Sqr(NumberArg). That is what the function returns.
If it does not return anything, and its just a method, you should declare it with Sub().
Sub()
'your method
End Sub

I need to solve an implicit equation in VBA

I want to give the other parameters that are mentioned in the function, and get a solution for a (the angle), but I get error: "invalid procedure call or argument" Run-time error 5.
I need to call the function in excel worksheet. It is a pretty long equation. Also, it could be that I enter a infinite loop but I don't know how to avoid that.
Function calculateangle(r, h, C, g, d, m, t, x, y As Single) As Single
Dim a As Single
a = 0
While y <> (d + r - r * Cos(a) + (x - (t - r + r * Sin(a))) * Tan(a) - (g
/ (2 * ((((C * m * (2 * g * (h - (d + r - r * Cos(a)))) ^
(1 / 2)) + m * (2 * g * (h - (d + r - r * Cos(a)))) ^ (1 / 2)) / (m +
0.04593)) ^ 2) * (Cos(a)) ^ 2)) * (x - (t - r + r * Sin(a))) ^ 2)
a = a + 0.01
Wend
MsgBox Round(a, 2)
End Function
One obvious issue is that you are using a Function but not returning a value.
This really is a complex piece of spaghetti! However, I suggest an approach like below which will help separate out various bits and thus make it easier to do debugging
Function calculateangle(<...all the bits ...>) As Double
Dim a As Double
Dim tTolerance as Double
dim f1 as Double ' sub sections to help untangle the spaghetti
Dim f2 as Double
Dim f3 as Double
Dim fFinal as Double
Dim tWithinTolerance as Boolean
tWithinTolerance = false
a = 0
tTolerance = 0.01
While not tWithinTolerance
f1 = d + r - r * Cos(a)
f2 = m*2*g*(h - f1)
f3 = x - (t - r + r * Sin(a))
fFinal = (f1 + f3 * Tan(a) - (g / (2 * ((((C * f2) ^
(1 / 2)) + f2 ^ (1 / 2)) / (m + 0.04593)) ^ 2) * (Cos(a)) ^ 2)) * f3 ^ 2)
tWithinTolerance = (Abs(y - fFinal) < tTolerance)
a = a + 0.01
Wend
Calculateangle = a ' note how this sets a return value for the function
End Function
I have left the rounding (which is a presentation issue) to the code that calls this function - this way you can display the answer to whatever level of detail you want!
(apologies if I have mangled any of the calculation on the way through - but you get the concept!)
For the author and those who want to deal with his solitaire. I hope I did not confuse anything in parentheses and simplifications.
Do
vCosA = Cos(a)
vCosADR = d + r * (1 - vCosA) ' d + r - r * vCosA '
vCosMGHADR = m * (2 * g * (h - vCosADR))
vSinAXTR = (x - (t - r * (1 - Sin(a)))) ' - r + r * Sin(a)
'((C * vCosMGHADR) + vCosMGHADR) == ((C + 1) * vCosMGHADR)
If (y = _
(vCosADR + vSinAXTR * Tan(a) - _
(g / _
(2 * _
( _
( _
((C + 1) * vCosMGHADR) / _
(m + 0.04593) _
) ^ 2 _
) * (vCosA ^ 2) _
) _
) * vSinAXTR ^ 2 _
)) Then Exit Do ' *** EXIT DO ***
a = a + 0.01
Loop

Gamma distribution, incomplete beta function

I need to calculate Gamma cumulative distribution, and it seems this is fairly equivalent to calculating the incomplete beta function.
Excel does have an inculded calculator, but I found no trace of the used algorithm.
Do any of you know an accurate way to calculate this function?
I tried the following, translated into VB.NET from a website, but it gives stupid results:
Function IncompleteBetaFunc(x As Double, a As Double, b As Double) As Double
If x <= 0 Or x >= 1 Then Return 0
Dim bt As Double
bt = Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x))
If x < (a + 1.0) / (a + b + 2.0) Then
Return bt * betacf(a, b, x) / a
Else
Return 1.0 - bt * betacf(b, a, 1.0 - x) / b
End If
End Function
Function betacf(x As Double, a As Double, b As Double) As Double
Const MAXIT As Integer = 100
Const EPS As Double = 0.0000003
Const FPMIN As Double = 1.0E-30
Dim aa, c, d, del, h, qab, qam, qap As Double
Dim m, m2 As Integer
qab = a + b
qap = a + 1.0
qam = a - 1.0
c = 1.0
d = 1.0 - qab * x / qap
If (Math.Abs(d) < FPMIN) Then d = FPMIN
d = 1.0 / d
h = d
For m = 1 To MAXIT
m2 = 2 * m
aa = m * (b - m) * x / ((qam + m2) * (a + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
h *= d * c
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
d = 1.0 + aa * d
If (Math.Abs(d) < FPMIN) Then d = FPMIN
c = 1.0 + aa / c
If (Math.Abs(c) < FPMIN) Then c = FPMIN
d = 1.0 / d
del = d * c
h *= del
If (Math.Abs(del - 1.0) < EPS) Then Exit For
Next
Return h
End Function
Thanks!
Meta.Numerics includes well-tested and performant code for this any many other special functions. Its incomplete Beta function is documented here. The underlying code can be studied here. It also has a full-on Gamma distribution object, which will give moments, generate random variates, and do other distribution-related stuff in addition to computing the CDF. The package available via NuGet; just search for Meta.Numerics in the VS NuGet interface.

updating values of outer loop, For loop in another For loop

My Question: I want to use the last calculated values of the inner loop (i) for outer loop (j). But it starts taking wrong values when (j=2 and i=1 ). where i might have a mistake?
I am not showing any inputs , just need help in understanding the outer loop.
For j = 1 To 4
wf = (2 * j - 1) * wff
For i = 1 To 3
PZ = (1 - GP / GT) * pzi
p = fPpz(PZ, ppc, Tpr)
z = p / PZ
pavg = (p + pwf) / 2
Bg_p = Bg(z, T, p, psc, Tsc)
mug_p = (mugas(SGg, p, T)) / 1000
mug_pavg = (mugas(SGg, pavg, T)) / 1000
Z_pavg = fZ(pavg / ppc, Tpr)
Bg_pavg = Bg(Z_pavg, T, pavg, psc, Tsc)
Time2 = 1 + Time1
delTime = Time2 - Time1
PIndex = PIndn(kres, hres, mug_pavg, Bg_pavg, JDf)
dpdlf = pf(p, pwf, xf, PIndex, tauyhb, nhb, khb, wfg_new, Hf, wf, Bg_pavg, dpdlold,mug_pavg, Bg_p, mug_p)
dpdlnew = dpdlf
If dpdlf < 0 Then
dpdlnew = dpdlold
End If
qg = FrGasF(tauyhb, nhb, khb, wfg_new, Hf, wf, dpdlnew, mug_p)
GP = 2 * qg * delTime * 60 / Bg_p + GP
qhb = FrHBF(tauyhb, nhb, khb, wfg_new, Hf, wf, dpdlnew)
wfg_old = qhb / xf / Hf * delTime * 60 / 2
'=======For next iteration (i+1)======='
wfg_new = wfg_new + wfg_old
Time1 = Time2
dpdlold = dpdlnew
Next i
Next j
Should you be setting wfg_new = 0 in the outer j loop? Otherwise, j=2, i=1 is using the wfg_new value from j=1, i=3 when you call the pffunction

vba excel: do something every time a certain variable is changed

I'm doing a bunch of stuff to the variable St
For i = 1 To 30000
Randomize
e1 = Rnd
e2 = Rnd
z1 = Sqr(-2 * Log(e1)) * Cos(2 * 3.14 * e2)
z2 = Sqr(-2 * Log(e1)) * Sin(2 * 3.14 * e2)
St = So * Exp((r - (sigma ^ 2) / 2) * T + sigma * Sqr(T) * z1)
C = C + Application.WorksheetFunction.Max(St - K, 0)
St = So * Exp((r - (sigma ^ 2) / 2) * T - sigma * Sqr(T) * z1)
C = C + Application.WorksheetFunction.Max(St - K, 0)
St = So * Exp((r - (sigma ^ 2) / 2) * T + sigma * Sqr(T) * z2)
C = C + Application.WorksheetFunction.Max(St - K, 0)
St = So * Exp((r - (sigma ^ 2) / 2) * T - sigma * Sqr(T) * z2)
C = C + Application.WorksheetFunction.Max(St - K, 0)
Next i
how do I get notified every time the variable changes?
In the Excel VBE, you can add a "watch" to each variable. Select your variable, go to the Debug menu, click Add Watch... and then under Watch Type, click Break When Value Changes.
maybe this a very naive answer but if you make a function that calls the sub then everytime a variable is changed then the function will be reevaluated.
you can use breakpoints or dump St's value after every evaluation to a column on your excel file.
a very annoying solution would be to add message boxes all over the place
yet another clever solution would be to add logic to your code by declarin a variable that would store the previous value of St and then compare against it after the evaluation of the new St