VBA results in #VALUE! only when For loop is used - excel

I am writing a function with a For loop, and ultimately the value of the function will depend on the output of the For loop. For now as a test, the value of the function is a constant. If the For loop is in the code, the function results in #Value!. If I remove the For loop, the output is the specified constant value. How does the For loop need to be specified to avoid this? Good values for Tc and Th as a test would be 100 and 300, respectively.
Function kndT(material As Integer, Tc As Double, Th As Double) As Variant
Dim x As Double
Select Case material
Case 4
If Th > 300 Then
Tmax = 300
Else
Tmax = Th
End If
A = 0.07918
b = 1.0957
c = -0.07277
D = 0.08084
e = 0.02803
f = -0.09464
g = 0.04179
h = -0.00571
i = 0
End Select
hh = (Tmax - Tc) / 999
fi = 0
nc = 1
For i = 1 To 999
Temp = (Tc + i * hh)
x = Log(Temp) / Log(10#)
y = A + b * x + c * x ^ 2 + D * x ^ 3 + e * x ^ 4 + f * x ^ 5 + g * x ^ 6 + h * x ^ 7 + i * x ^ 8
fn = 10 ^ y
If nc = 3 Then
fi = fi + 2 * fn
nc = 1
Else
fi = fi + 3 * fn
nc = nc + 1
End If
Next i
kndT = 2
End Function

Related

UDF value error for complex itterative function

I want to create an UDF for my excel worksheet. However when I select my input for my UDF I get a value error. Can someone help me?
Code and images below:
code:
Public Function pipediameter(Pth As Double) As Double
Dim D As Double
p = 150
Tv = 30
T = 55
k = 0.000045
vi = 4
a = (1.729 * (10 ^ (-6))) / ((1 + (T / 25)) ^ 1.165)
rho = 988
Cp = 4180
di = 1
i = 1
While i <= 10
v = 4 * Pth / (3.14 * di ^ 2 * Cp * rho * Tv)
Re = v * di / vi
B1 = (0.774 * Log(Re) - 1.41) / (1 + 1.32 * Sqr(k / di))
B2 = ((k * Re) / (3.7 * di)) + (2.51 * B1)
Y = (B1 - ((B1 + (2 * (Log(B2 / Re) / Log(10)))) / (1 + (2.18 / B2)))) ^ (-2)
D = (((8 * Y) / (p * rho)) * ((Pth) / (3.14 * Cp * Tv)) ^ 2) ^ 0.2
di = D
i = i + 1
Wend
pipediameter = D
End Function

I want to display an approximation of pi making use of only the first five terms from the infinite series that helps compute pi

pi = 3 + (4/(2x3x4)) - (4/(4x5x6)) + (4/(6x7x8)) - (4/(8x9x10)) + (4/(10x11x12)) - (4/(12x13x14)) + (4/(14x15x16)) + ...
I tried a little something but the output shows something different.
a, b, c = 2, 3, 4
x = 4/(a*b*c)
pi = 3
for i in range(5):
pi += x
a = c
b = a + 1
c = b + 1
x *= -1
print(pi)
a, b, c = 2, 3, 4
four = 4
pi = 3
for i in range(5):
pi += four/(a*b*c)
a = c
b = a + 1
c = b + 1
four *= -1
print(pi)

How can I get the criteria to change in a CountIf function in VBA as a loop progresses?

week = 1
y = 3
z = 18
For l = LBound(arr1) To UBound(arr1)
Range("A1").Value = WorksheetFunction.CountIfs(Range("B2:B250"), week)
x = Range("A1").Value
pf1 = "=IF("
pf2 = l
pf3 = "3=1,VLOOKUP($L3,$C$"
pf4 = y
pf5 = ":$F$"
pf6 = z
pf7 = ",3,FALSE),VLOOKUP($L3,$D$"
pf8 = ":$F$"
pf9 = ",3,FALSE))"
pf = pf1 + pf2 + pf3 + pf4 + pf5 + pf6 + pf7 + pf4 + pf8 + pf6 + pf9
If x = 14 Then
y = 14 + y
z = 13 + z
End If
If x = 15 Then
y = 15 + y
z = 14 + z
End If
If x = 16 Then
y = 16 + y
z = 15 + z
End If
pfrng.Offset(0, j * 6) = pf
j = j + 1
week = week + 1
Range("A1").ClearContents
Next l
I want the script above to recognize and change the value of the variable "week" as I loop through the lower bound of my array (arr1) to the upper bound. While doing so, it must also increase the y and z values accordingly.
Also, is this the correct method I should be using, or is there something better/more efficient or something with a cleaner appearance?
Thank you in advance!

Solving a line intercept equation

What is A and B so that the line Ay = Bx + 1 passes through points (1, 3) and (5,13) in the Cartesian plane?
I have been trying to solve it using the slope intercept equation to no avail. This is taken from Dale Hoffman's Contemprary Calculus.
First, I would reorder to get canonical form,
y = (B/A) * x + (1/A) = m * x + b
Now we find slope (m):
m = dy / dx = (13 - 3) / (5 - 1) = 2.5
sub in to find b:
3 = 2.5 * 1 + b
b = 0.5
Now sub back to find the values you want,
b = 0.5 = 1 / A
A = 2
m = 2.5 = B / 2
B = 5

Run Time error 9:Array

I am new to VBA programming and am trying to develop a simple code for RCC design. Most of the values are assigned directly from the excel sheet. I am getting this error that says "division by zero".The line within ** ** is highlighted while debugging. it seems there is some problem with declaration or looping but i am not being able to identify. Pls help. Thanx in advance. The code is as follows:
Private Sub CommandButton1_Click()
Dim a As Double, b As Double, result As String, Mu As Double
Dim i As Integer, j As Integer, c As Integer, Xu1, Xu, es, d, f, fs As Double
Dim strain1(1 To 6) As Double, stress1(1 To 6) As Double
a = Range("E30").Value
b = Range("O30").Value
If a < b Then
result = "Under Reinforced Section"
Mu = Range("E32").Value * Range("E34").Value
ElseIf a = b Then
result = "Balanced Secction"
Mu = Range("E32").Value * Range("E34").Value
ElseIf a > b Then
result = "Over Reinforced Section"
j = 31
For i = 1 To 6
strain1(i) = Cells(j, 7)// loop to assign values in array from excel sheet
j = j + 1
Next
j = 31
For i = 1 To 6
stress1(i) = Cells(j, 8)
j = j + 1
Next
c = 1
Xu1 = Range("O30").Value
d = Range("E31").Value
Do While c = 1
Xu = Xu1
**es = 0.0035 * (d - Xu) / (Xu)**// Shows error apparently Xu is taking value zero
If Range("E22").Value = 250 Then
fs = es * Range("E23").Value
f = 0.87 * Range("E22").Value
If fs > f Then
fs = f
End If
ElseIf Range("E22").Value = 415 Then
f = 0.696 * Range("E22").Value / Range("E23").Value
If es > f Then
For i = 1 To 6
If es > strain1(i) And es < strain1(i + 1) Then// to locate es in the array and then interpolate
fs = stress1(i) + ((stress1(i + 1) - stress1(i)) / (strain1(i + 1) - strain1(i))) * (es - strain1(i))// linear interpolation formulae
End If
Next
ElseIf es < f Then
fs = es * Range("E23").Value
End If
Xu1 = Range("O29").Value * fs / (0.36 * Range("E21").Value * Range("E16").Value)
If Xu1 = Xu Then
c = 0
End If
Mu = 0.36 * Range("E21").Value * Range("E16").Value * Xu1 * Range("E34").Value
End If
Loop
End If
Range("O21").Value = Mu
MsgBox result
End Sub
strain1(1 To 6) has 6 elements 1 to 6, for i=6 you're trying to access a 7th element (strain1(i + 1)) in the highlighted row. (the same holds true for stress1 in the next line)

Resources