simple math in userform textbox

inactiveUser214710

Board Regular
Joined
Apr 27, 2012
Messages
171
hello everybody!
I have two textboxes to introduce numbers (txt 1;txt 2), and a third textbox where appear automatically the result (for exemple)of the sum of (txt1;text 2). how can i do that?
Thank you
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If you have some basic idea of userforms (like me) :biggrin:

Something like this:


Private Sub CommandButton1_Click()
Dim x, y, z As Long
x = Val(Me.TextBox1.Value)
y = Val(Me.TextBox2.Value)
z = x + y
Me.TextBox3.Value = z
End Sub
 
Upvote 0
Or simply

Code:
Private Sub CommandButton1_Click()
Me.TextBox3.Value = Val(Me.TextBox1.Value) + Val(Me.TextBox2.Value)
End Sub

Note that

Code:
Dim x, y, z As Long

declares x and y as Variant and z as Long.
 
Upvote 0
I appreciate the quick response as well the information that was useful to me, however what I want is,when we enter data in (textbox1 and textbox2), automatically arises its value (the mathematical operation used) into TextBox3, without need to do "enter".
Thanks in advance
Private Sub TextBox3_Enter()
Dim x, y, z As Long
x = Val(Me.TextBox1.Value)
y = Val(Me.TextBox2.Value)
z = x + y
Me.TextBox3.Value = z

z = Val(Me.TextBox1.Value) + Val(Me.TextBox2.Value)

End Sub
 
Upvote 0
Try this in the userform's code module

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox3.Value = Val(Me.TextBox1.Value) + Val(Me.TextBox2.Value)
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox3.Value = Val(Me.TextBox1.Value) + Val(Me.TextBox2.Value)
End Sub
 
Upvote 0
Or simply

Code:
Private Sub CommandButton1_Click()
Me.TextBox3.Value = Val(Me.TextBox1.Value) + Val(Me.TextBox2.Value)
End Sub

Note that

Code:
Dim x, y, z As Long

declares x and y as Variant and z as Long.

Thanks, I wasn't aware of it :)
 
Upvote 0
hi
almost perfect
when I put the value in the box1, the box3 shows the correct value =ok.
when I put another value in the box 2, the box 3 keeps the previous value, so I need to click in box1 for box3 show the sum of the two boxes.
what I need to do for, after put the value in box2, the box3 give the correct value
thank you for your patience
 
Upvote 0
Just change the value in textbox1 or textbox2 then click in textbox 3.
 
Upvote 0
hi
Sorry I do not to understand, but what do you mean by change the value. Is it on texbox propriety or is its value (txtbox1.value), to change for text (textbox1.text)?
if it's text property, which one is it?
Thank you
 
Upvote 0

Forum statistics

Threads
1,203,072
Messages
6,053,377
Members
444,659
Latest member
vuphihung

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top