ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding values in textboxes... (https://www.excelbanter.com/excel-programming/305156-re-adding-values-textboxes.html)

Jake Marx[_3_]

Adding values in textboxes...
 
Hi Gary,

Gary Phillips wrote:
I've got four textboxes... The first three are enabled, the fourth is
not.

I want the user to be able to enter an integer value into the first
three and it will add those three values into the fourth box. I do
not want the user to have to press any buttons, I want it to update
as the user goes along.


Something like this should work for you:

Private Sub TextBox1_Change()
CalcTotal
End Sub

Private Sub TextBox2_Change()
CalcTotal
End Sub

Private Sub TextBox3_Change()
CalcTotal
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As _
MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii Asc("9") Then
Interaction.Beep
KeyAscii = 0
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As _
MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii Asc("9") Then
Interaction.Beep
KeyAscii = 0
End If
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As _
MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii Asc("9") Then
Interaction.Beep
KeyAscii = 0
End If
End Sub

Private Sub CalcTotal()
TextBox4.Text = CLng("0" & TextBox1.Text) + _
CLng("0" & TextBox2.Text) + _
CLng("0" & TextBox3.Text)
End Sub


You'll probably want to set the MaxLength property of the 3 textboxes to
something manageable to avoid overflow errors.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

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



All times are GMT +1. The time now is 05:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com