How can I fix the Run-time error '5': invalid procedure call or argument - vba

How can I fix the
Run-time error '5': invalid procedure call or argument
I have a this line in my code: Betac = ((t - t0) / (Betah(h, RH, alfa3) + (t - t0))) ^ 0.3.
It runs correctly when I enter the h parameter by myself, but when I input it by variable VBA shows this error.
please help!
' ????? if I input h here runs correctly
' but when input it in the function argument vba gives run time error '5'
h = 777
RH = Cells(26, 11)
Betafcm = (16.8 / Sqr(fcm))
Betat0 = 1 / (0.1 + t0 ^ 0.2)
alfa1 = (35 / fcm) ^ 0.7
alfa2 = (35 / fcm) ^ 0.2
alfa3 = (35 / fcm) ^ 0.5
Betac = ((t - t0) / (Betah(h, RH, alfa3) + (t - t0))) ^ 0.3

Related

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

(VBA) Square equalities - Sub or function not defined

I'm facing some problems with my exercise test in uni, but after editing some code it seems like something is wrong there.
Public Function Uzd(x) As String
If x = 1 Or x = 2 Then
Uzd = (2 * x + 3) / sqrt(x ^ 2 + 3 * x + 2)
Else
Uzd = "Incorrect data"
End If
End Function
After running code I get - Sub or function not defined
The function you're trying to use is located in the VBA type library, in the Math module:
You can avoid such typos by using IntelliSense and fully-qualifying the global-scope functions you use:
Uzd = (2 * x + 3) / Math.Sqr(x ^ 2 + 3 * x + 2)

I Keep getting a #value error in Excel 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

Finding a point on a diagonal line when i have the start point and end point of the Line

Hi am looking for some help
I have a Diagonal line drawn on a picture box on my forum and i need to know if the user has clicked the line
I have the Start point and End Point of the Line and the mouse x,y location
So i basically need to find out if the x,y of the mouse is on the line.
can anyone help?
Thanks
Example: Line Start point (A) is (0, 0), END point (B) is (10, 5).
Slope of line is therefore:
m(slope) = (y2 - y1) / (x2 - x1)
= (5 - 0) / (10 - 0)
= 5 / 10
= 0.5
To check if your point(x,y) (C) is on the line it must have the same slope from A->C and C->B. so do the same calculation again. Say point is (4, 2)
m(AC) = (2 - 0) / (4 - 0)
= 2 / 4
= 0.5
m(CB) = (5 - 2) / (10 - 4)
= 3 / 6
= 0.5
Therefore this point would be on line AB.
If point was (20, 10)
m(AC) = (10 - 0) / (20 - 0)
= 10 / 20
= 0.5
However:
m(CB) = (5 - 10) / (10 - 20)
= -5 / -10
= -0.5
Similarly if point was (2, 2)
m(AC) = (2 - 0) / (2 - 0)
= 2 / 2
= 1
m(CB) = (5 - 2) / (10 - 2)
= 3 / 8
= 0.375
So for a point to be on a line m(AB) == m(AC) == m(CB)
You may have a bit of work arounds to perform as you may not be able to get decimal values, and your line may be more than one pixel in width, but these basic principles should see you through.
Given two points, (2,4) and (-1,-2) determine the slope intercept form of the line.
1. Determine the slope
y1-y2 4-(-2) 6
----- = ------= --- = 2 = M
x1-x2 2-(-1) 3
2. To slope intercept form using one of the original points and slope from above.
(y - y1) = m(x - x1)
(y - 4) = 2(x - 2)
y - 4 = 2x - 4
y = 2x + 0 (0 is y intercept)
y = 2x (y = 2x + 0) is in slope intercept form
3. To determine if a point lies on the line, plug and chug with the new point.
new point (1,2) does y = 2x? 2 = 2(1) = true so (1,2) is on the line.
new point (2,2) does y = 2x? 2 = 2(2) = false so (2,2) is not on the line.
In your original problem you said line, but I think you might mean line segment. If you mean the latter you will also need to verify that the new x and y are within the bounds of the given segment.
The code will look something like this
Dim pta As Point = New Point(2, 4)
Dim ptb As Point = New Point(-1, -2)
Dim M As Double
If pta.X - ptb.X <> 0 Then
M = (pta.Y - ptb.Y) / (pta.X - ptb.X)
End If
'(y - pta.y) = M(x - pta.x)
'y - pta.y = Mx - m(pta.x)
'y = Mx - M(pta.x) + pta.y
Dim yIntercept As Double = (-M * pta.X) + pta.Y
Dim ptN1 As Point = New Point(1, 2)
Dim ptN2 As Point = New Point(2, 2)
If ptN1.Y = (M * (ptN1.X)) + yIntercept Then
Stop
Else
Stop
End If
If ptN2.Y = (M * (ptN2.X)) + yIntercept Then
Stop
Else
Stop
End If

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