Sum numbers in userform

H

helmekki

Hi there
i use userform to sum the entered numbers into the same userform


Code:
--------------------
Private Sub CommandButton1_Click()

If Label1.Caption = "" Then

Label1.Caption = TextBox1.Value
TextBox2.Value = TextBox1.Value

Else:
Label1.Caption = Label1.Caption & vbCr
Label1.Caption = Label1.Caption & TextBox1.Value
TextBox2.Value = TextBox2.Value + TextBox1.Value

End If

TextBox1.Value = ""

End Sub
--------------------



but this line:

Code:
--------------------
TextBox2.Value = TextBox2.Value + TextBox1.Value
--------------------


put the numeric values next to each other

i need to sum them insted ?
 
N

Norman Jones

Hi Helmekki,
but this line:

Code:
--------------------
TextBox2.Value = TextBox2.Value + TextBox1.Value
--------------------


put the numeric values next to each other

i need to sum them insted ?

Try:

TextBox2.Value = CDbl(TextBox2.Value) + CDbl(TextBox1.Value)
 
H

helmekki

i tried with this code:

Code:
--------------------
Private Sub CommandButton1_Click()
Dim tooo As Double
Dim vtt As Double

If Label1.Caption = "" Then
Label1.Caption = vtt.Value
tooo.Value = vtt.Value

Else:
Label1.Caption = Label1.Caption & vbCr
Label1.Caption = Label1.Caption & vtt
tooo.Value = CDbl(tooo) + CDbl(vtt)


End If

tooo.Value = ""

End Sub
--------------------

but it gives me invaled qualifier when tring to read vtt

any idea how to solve the problem ?
 
C

Chip Pearson

Since vtt is an intrinsic data type (a double), it doesn't have
any properties. Your code

tooo.Value = vtt.Value
is wrong. It should be
tooo = vtt

The code

tooo.Value = CDbl(tooo) + CDbl(vtt)
is also wrong. First, you can't have a Value property, and since
tooo and vtt are declared as double, you don't need the CDbl
function at all.
tooo= tooo + vtt


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"helmekki"
message
news:[email protected]...
 
H

helmekki

I made some correction, but still got type mismatch in the line

CDbl(tooo.Value)

here is the whole code


Code:
--------------------

Private Sub CommandButton1_Click()
Dim cdbltooo As Double
Dim cdbltotv As Double

If Label1.Caption = "" Then
Label1.Caption = totv.Value
tooo.Value = CDbl(totv.Value)

Else:
Label1.Caption = Label1.Caption & vbCr
Label1.Caption = Label1.Caption & totv.Value
tooo.Value = CDbl(tooo.Value) + CDbl(totv)


End If

tooo.Value = ""

End Sub
 
C

Chip Pearson

Did you read my reply? You still have .Value on your totv and
tooo variables. This WILL NOT WORK. Get rid of the .Value's. And
you don't need the CDbl functions because your variables are
already doubles.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"helmekki"
message
news:[email protected]...
 
H

helmekki

Ok , i did what you said, but it did not work with me .
still tooo TextBox does not show the total amount entered in vtt each
time i enter a new number.............
i need the tooo to show the total of numbers entered into vtt each
time

here is the code and please find the attached file


Code:
--------------------
Private Sub CommandButton1_Click()
Dim tooo As Double
Dim vtt As Double

If Label1.Caption = "" Then
Label1.Caption = vtt
tooo = vtt

Else:
Label1.Caption = Label1.Caption & vbCr
Label1.Caption = Label1.Caption & vtt
tooo = tooo + vtt

End If
tooo = ""

End Sub
 
T

Tom Ogilvy

Your last command is

tooo = ""

so it you clear whatever it may have shown from the earlier code.

Also, don't dimension tooo as Double if it is a textbox. Don't dimension it
at all.

Assuming tooo and vtt are textboxes on your form

Private Sub CommandButton1_Click()

If Label1.Caption = "" Then
Label1.Caption = vtt
tooo.Value = vtt.Value

Else:
Label1.Caption = Label1.Caption & vbCr
Label1.Caption = Label1.Caption & vtt
tooo.Value = val(tooo.value) + val(vtt.Value)

End If
End Sub
 

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