Calculating TextBoxes

  • Thread starter Thread starter Patrick C. Simonds
  • Start date Start date
P

Patrick C. Simonds

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

Is this possible?
 
Sure, put the calculation code in the Textbox12_Change event.

HTH. Best wishes Harald
 
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.
 

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

Back
Top