Text Boxes

J

Jamal

I was following a question, which was posted to this group
about two weeks ago on summing up the values of text boxes
into another text box. I tried the following:

TextboxTotal = T1+T2+T3 'T being the textboxes

instead of adding the numerical values it just places them
side by side. So the contents of Textboxtotal becomes
100200300 instead of 600. However when i did the following
it works:

TextboxTotal = (T1*1)+(T2*1)+(T3*1)

it seems the contents of each box is not being treated as
numbers.

I am using Excel 2000

Is this a bug or I am missing something? Thanks. It is
just bugging me.
 
D

DennisE

The contents of a textbox is just that, text. So when you write Total.ext =
TextBox1.text + TextBox2.text + TextBox3.text, Excel believes you are asking
for concatenation, just like "Apple" + "Pie" = "Apple Pie".
What you should be doing to get an arithmetic sum is writing Total.Value=
TextBox1.Value+TextBox2.Value+TextBox3.Value

-- Dennis Eisen
 
J

Jamal

Hi Dennis,

T1.value+t2.value concatenate the result. I have tried
with text and value. Same result. Anyway, thanks for your
interest and suggestion. Cheers.
 
J

Jake Marx

Hi Jamal,

You can use conversion functions to convert the text strings into numeric
values. Here's an example that will work (assuming your "total" TextBox is
named "TextBox4"):

Private Sub CommandButton1_Click()
Dim dblTotal As Double
Dim ctl As Control

For Each ctl In Controls
If TypeOf ctl Is MSForms.TextBox Then
If ctl.Name <> "TextBox4" Then
If IsNumeric(ctl.Object.Text) Then
dblTotal = dblTotal + _
CDbl(ctl.Object.Text)
End If
End If
End If
Next ctl

TextBox4.Text = Format$(dblTotal, "#,##0.00")
End Sub


This will add up all values in TextBoxes on the Userform. It ignores
non-numeric values.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top