Search code examples
vbaexcelmismatch

vba error 13 type missmatch, VBA


New to using VBA and know little about it. Using it for an Engineering project to solve deep seated slope stability problems. When entering in the first 7 calculations VBA solves these correctly in a user form. But when I try to calculate the 8th formula (AVHS8) it gives me an error, witch is run time error "13" type mismatch. I am unsure what the problem is, the formulas work in excel perfectly. if anyone could help it would be much appreciated. The problem is below.

BES = 2.5 RAD = 18.58 DTCP = 7.5 and OH =17, these are just numbers that are set and do not change

Private Sub Calculate_Click()
RAD = Sqr(DTCP ^ 2 + OH ^ 2)
BES = DTCP / 3
AVHS1 = (0)
AVHS2 = (Sqr(RAD ^ 2 - (DTCP - BES) ^ 2) - OH)
AVHS3 = (Sqr(RAD ^ 2 - (DTCP - BES - BES) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES))
AVHS4 = (Sqr(RAD ^ 2 - (DTCP - BES - BES - BES) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES * 2))
AVHS5 = (Sqr(RAD ^ 2 - (DTCP - BES - BES) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES * 3))
AVHS6 = (Sqr(RAD ^ 2 - (DTCP - BES) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES * 4))
AVHS7 = (Sqr(RAD ^ 2 - (DTCP) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES * 5))
'VBA can solve all the above code. it cant solve the code below (AVHS8)
AVHS8 = (Sqr(RAD ^ 2 - (DTCP + BES) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES * 6))

Solution

  • I assume those are controls on your form rather than variables, which would explain the problem. As controls, the contents are text and when you use (DTCP + BES) that is actually returning 7.52.5 rather than 10, because + will also work as a concatenation operator. Change the last line to:

    AVHS8 = (Sqr(RAD ^ 2 - (CDbl(DTCP) + CDbl(BES)) ^ 2) - (OH) + (Tan(30 * 3.14 / 180) * BES * 6))