• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to get sum value of two text boxes in third one

I have a question about text boxes sum.

How it is possible If i have three text boxes on a userform and want to diplay the sum of two textboxes in the third text box(or label) with out a button.

For example if i enter value 2 in the first text box,the third text box show 2 and further when i enter 5 in the 2nd text box , the third text box show 7.

Please explain with example.

Thanks
 
Yes, it's possible. But you need to specify when you want the calculation to take place.

1. When you click on specific command button.
Then you'd assign it to commandbutton_click event.

2. When you enter value in textbox2.
Then you'd assign it to Textbox2_Change event.

etc etc.

However, textbox as name suggests, stores value as Text. So you will need to use CDbl() to coerce it into double type.

Sample Code:
Code:
Private Sub TextBox2_Change()
TextBox3.Value = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End Sub
 
You should also have a module so if a user enters data in textbox2 first, then textbox1 it will update

Code:
Private Sub TextBox1_Change()
TextBox3.Value = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End Sub

You should also setup the code so that the two Textboxes are cleared or reset on initialisation

Code:
Private Sub UserForm_Initialize()
TextBox1.Value = vbNullString
TextBox2.Value = vbNullString
End Sub
 
The code is partially help me .When i put data directly or overwrite textbox2, it gives me error .I am upload my file please guide me a little more according to my file.
 

Attachments

  • sumTxtbxes.xlsm
    13.8 KB · Views: 4
Code:
Private Sub TextBox1_Change()
If Len(TextBox2) = 0 Then
    TextBox3 = CDbl(TextBox1.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub

Private Sub TextBox2_Change()
If Len(TextBox1) = 0 Then
    TextBox3 = CDbl(TextBox2.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub
Private Sub UserForm_Initialize()
    Application.EnableEvents = False
    TextBox1.Value = vbNullString
    TextBox2.Value = vbNullString
    Application.EnableEvents = True
End Sub
 
Code:
Private Sub TextBox1_Change()
If Len(TextBox2) = 0 Then
    TextBox3 = CDbl(TextBox1.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub

Private Sub TextBox2_Change()
If Len(TextBox1) = 0 Then
    TextBox3 = CDbl(TextBox2.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub
Private Sub UserForm_Initialize()
    Application.EnableEvents = False
    TextBox1.Value = vbNullString
    TextBox2.Value = vbNullString
    Application.EnableEvents = True
End Sub
Code:
Private Sub TextBox1_Change()
If Len(TextBox2) = 0 Then
    TextBox3 = CDbl(TextBox1.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub

Private Sub TextBox2_Change()
If Len(TextBox1) = 0 Then
    TextBox3 = CDbl(TextBox2.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub
Private Sub UserForm_Initialize()
    Application.EnableEvents = False
    TextBox1.Value = vbNullString
    TextBox2.Value = vbNullString
    Application.EnableEvents = True
End Sub

Sorry to disturb you again.The code is almost ok but when i put value in textbox2 for the first time , it is ok but when i re write the value in text box2,it gives error.
For example when i write 4 in text box1 and 2 in text box2 ,the text box3 display 6.But when i want to change the value 2 in textbox2 then code goes wrong and gives error.
 
You probably tried to delete previous entry.

Just highlight and enter new value with out deleting.

Error is caused by trying to convert null string to double.

If you want to avoid that... just add additional IF statement in Sub TextBox2_Change().
Code:
Private Sub TextBox2_Change()
If Not IsNumeric(TextBox2.Value) Then Exit Sub
If Len(TextBox1) = 0 Then
    TextBox3 = CDbl(TextBox2.Value)
Else
    TextBox3 = CDbl(TextBox1.Value) + CDbl(TextBox2.Value)
End If
End Sub

Exact method to deal with this, will depend on how you want it to behave.
 
Back
Top