https://stackoverflow.com/questions/...or-my-function
Please read the FAQ about multi-posting
Are you sure your algorithm is correct? (My first macro was a little off since RH15 does change within the Do Loop)
RH15, VCF15 and RHO15 are the only variables that change
T15, RHO, K0, K1, and A don't change, so they can be outside of the Do Loop
On the marked line, RHO15 on the first time is = 0 since it's never assigned a value, so RH15 also = 0
I think that would cause a divide / 0 on the line alpha15 with K1 / RH15
When the loop finishes, it should return the last value of rho15 after the error <= 0.05
What is the algorithm for, and do you have any test data with correct answers ?
Function TADegC(DEN, Temp As Double)
Application.Volatile
T15 = temp - 15
RHO = DEN
RH15 = RHO * 1000
Do
RH15 = RHO15 '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
K0 = 613.9723
K1 = 0
A = 0
alpha15 = (K0 / (RH15 ^ 2)) + (K1 / RH15) + A
VCF15 = Exp((-alpha15 * T15) * (1 + 0.8 * alpha15 * T15))
rho15 = RH15 / VCF15
Error = Abs(rho15 - RH15)
Loop until error <= 0.05
TADegC = rho15
End Function