As the error message says, it's a type mismatch. The variables Nbr1 and Nbr2
are declared as Double. The TextBox1 and TextBox2 are objects that represent
the textbox controls in the userform. You can sort of get away with using
just TextBox1 because the .Text property is the "default property" for that
object, but it's best to use the complete expression TextBox1.Text, which
returns a String value.
Then you must do the explicit type conversions. There are two ways to do
that:
- The CDbl function takes a string argument and converts it to a Double
value. But if the string doesn't satisfy the rules for a number that can be
converted, you'll get an error. That means you have to use an On Error
statement that goes to a piece of error handling code (look in the help
about On Error) or else if someone enters a meaningless value the userform
will show an error message and stop like it does now.
On Error GoTo ErrHdl
Nbr1 = CDbl(TextBox1.Text)
Nbr2 = CDbl(TextBox2.Text)
....
Exit Sub
ErrHdl:
' put error handling code here
End Sub
- The Val function takes a string argument and converts it to a number
(maybe an Integer or Long or Single or Double, depending on what's in the
string), and then the assignment to the Double on the left of the equal sign
will do a silent conversion to Double. If the string doesn't look like a
number, the Val function returns a zero, so you'd have to look for that and
maybe treat it as an error (depends on what the text box represents).
Nbr1 = Val(TextBox1.Text)
' If Nbr1 = 0 Then .... End If
Nbr2 = Val(TextBox2.Text)
' If Nbr2 = 0 Then .... End If
Finally, after you add the numbers together, you should do the reverse
conversion to put the value into the third text box:
TextBox29.Text = CStr(GTNbr1)
Also, you neglected to declare GTNbr1 As Double; that means it defaults to
being a Variant. It's a good idea to force all variables to be declared --
see
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm.