ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Calculating TextBoxes (https://www.excelbanter.com/excel-programming/422247-calculating-textboxes.html)

Patrick C. Simonds

Calculating TextBoxes
 
When I change TextBox12 I would like the value of TextBox13 to be
TextBox12 - TextBox11/24 .

Is this possible?


Harald Staff[_2_]

Calculating TextBoxes
 
Sure, put the calculation code in the Textbox12_Change event.

HTH. Best wishes Harald

"Patrick C. Simonds" wrote in message
...
When I change TextBox12 I would like the value of TextBox13 to be
TextBox12 - TextBox11/24 .

Is this possible?



Dave Peterson

Calculating TextBoxes
 
I created a small userform with 3 textboxes, a label and two commandbuttons.

This seemed to work ok:

Option Explicit
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If DoCalc(Me.TextBox1) = True Then
'ok
Else
Cancel = True
End If
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If DoCalc(Me.TextBox2) = True Then
'ok
Else
Cancel = True
End If
End Sub
Private Sub UserForm_Initialize()

With Me.Label1
.Caption = ""
.ForeColor = vbRed
End With

Me.TextBox1.Tag = "ID here"
Me.TextBox2.Tag = "Other ID here"

With Me.CommandButton1
.Caption = "Ok"
.Default = True
End With

With Me.CommandButton2
.Caption = "Cancel"
.Cancel = True
.TakeFocusOnClick = False
End With

End Sub
Function DoCalc(myTB As MSForms.TextBox) As Boolean
If IsNumeric("0" & Me.TextBox1.Value) _
And IsNumeric("0" & Me.TextBox2.Value) Then
Me.TextBox3.Value = CDbl("0" & Me.TextBox1.Value) _
- (CDbl("0" & Me.TextBox2.Value) / 24)
Me.Label1.Caption = ""
DoCalc = True
Else
Me.Label1.Caption = "Please enter a number in: " & myTB.Tag
DoCalc = False
End If
End Function

I used this kind of thing:
IsNumeric("0" & Me.TextBox1.Value)
so that an empty textbox would be treated as 0.

"Patrick C. Simonds" wrote:

When I change TextBox12 I would like the value of TextBox13 to be
TextBox12 - TextBox11/24 .

Is this possible?


--

Dave Peterson


All times are GMT +1. The time now is 02:27 PM.

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