Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I have text boxes and labels that show values, using formulas such as
the code below for Label7: Private Sub TextBox2_Change() Label7.Caption = Application.Text(CDbl(TextBox1.Value - TextBox2.Value) / (TextBox3.Value - 1), "# ??/16") End Sub If I am in textbox2 and enter a value, it works fine, if I want to change the value by pressing back space I get a error, because (I assume, it becomes a zero error) ...is there a way to trap this error and still be able to stay in the textbox? |
#2
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Private Sub TextBox2_Change()
Dim tmp If TextBox1.Value = "" Or TextBox3.Value = "" Then MsgBox "Incomplete data" Else tmp = CDbl(TextBox1.Value) If TextBox2.Text < "" Then tmp = tmp + CDbl(TextBox2.Value) End If tmp = tmp / CDbl(TextBox3.Value - 1) Label7.Caption = Application.Text(tmp, "# ??/16") End If End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "damorrison" wrote in message oups.com... I have text boxes and labels that show values, using formulas such as the code below for Label7: Private Sub TextBox2_Change() Label7.Caption = Application.Text(CDbl(TextBox1.Value - TextBox2.Value) / (TextBox3.Value - 1), "# ??/16") End Sub If I am in textbox2 and enter a value, it works fine, if I want to change the value by pressing back space I get a error, because (I assume, it becomes a zero error) ..is there a way to trap this error and still be able to stay in the textbox? |
#3
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I think I'd check to make sure everything was numeric and that textbox3.value
was different than 1 (no div/0 errors): Option Explicit Private Sub TextBox2_Change() If IsNumeric(Me.TextBox1.Value) _ And IsNumeric(Me.TextBox2.Value) _ And IsNumeric(Me.TextBox3.Value) Then If Me.TextBox3.Value < 1 Then Me.Label7.Caption _ = Format(CDbl(TextBox1.Value - TextBox2.Value) _ / (TextBox3.Value - 1), "# ??/16") Else Me.Label7.Caption = "" End If End If End Sub And Format worked ok for me instead of application.text--not all number formats can be done this way, but this one was ok. damorrison wrote: I have text boxes and labels that show values, using formulas such as the code below for Label7: Private Sub TextBox2_Change() Label7.Caption = Application.Text(CDbl(TextBox1.Value - TextBox2.Value) / (TextBox3.Value - 1), "# ??/16") End Sub If I am in textbox2 and enter a value, it works fine, if I want to change the value by pressing back space I get a error, because (I assume, it becomes a zero error) ..is there a way to trap this error and still be able to stay in the textbox? -- Dave Peterson |
#4
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Just a slight rearrangement of code to fix the caption if there are errors:
Option Explicit Private Sub TextBox2_Change() Me.Label7.Caption = "" If IsNumeric(Me.TextBox1.Value) _ And IsNumeric(Me.TextBox2.Value) _ And IsNumeric(Me.TextBox3.Value) Then If Me.TextBox3.Value < 1 Then Me.Label7.Caption _ = Format(CDbl(TextBox1.Value - TextBox2.Value) _ / (TextBox3.Value - 1), "# ??/16") End If End If End Sub Dave Peterson wrote: I think I'd check to make sure everything was numeric and that textbox3.value was different than 1 (no div/0 errors): Option Explicit Private Sub TextBox2_Change() If IsNumeric(Me.TextBox1.Value) _ And IsNumeric(Me.TextBox2.Value) _ And IsNumeric(Me.TextBox3.Value) Then If Me.TextBox3.Value < 1 Then Me.Label7.Caption _ = Format(CDbl(TextBox1.Value - TextBox2.Value) _ / (TextBox3.Value - 1), "# ??/16") Else Me.Label7.Caption = "" End If End If End Sub And Format worked ok for me instead of application.text--not all number formats can be done this way, but this one was ok. damorrison wrote: I have text boxes and labels that show values, using formulas such as the code below for Label7: Private Sub TextBox2_Change() Label7.Caption = Application.Text(CDbl(TextBox1.Value - TextBox2.Value) / (TextBox3.Value - 1), "# ??/16") End Sub If I am in textbox2 and enter a value, it works fine, if I want to change the value by pressing back space I get a error, because (I assume, it becomes a zero error) ..is there a way to trap this error and still be able to stay in the textbox? -- Dave Peterson -- Dave Peterson |
#5
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Thanks, this works
tmp = tmp + CDbl(TextBox2.Value) should have been tmp = tmp - CDbl(TextBox2.Value) Dave, your code displays the whole number then ??/16 (for the fraction) in the label I don't know why |
#6
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
Use application.text() instead of format(). (I didn't notice it before.)
damorrison wrote: Thanks, this works tmp = tmp + CDbl(TextBox2.Value) should have been tmp = tmp - CDbl(TextBox2.Value) Dave, your code displays the whole number then ??/16 (for the fraction) in the label I don't know why -- Dave Peterson |
#7
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I didn't notice that, either
Thanks |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
userform that add data in all w/sheets | Excel Discussion (Misc queries) | |||
Convert Worksheet to Userform | Excel Discussion (Misc queries) | |||
Data Validation Cell - Move to UserForm | Excel Worksheet Functions | |||
Cell Content from UserForm Not Retained | Excel Discussion (Misc queries) | |||
How can I run a macro in the background whilst a UserForm is visib | Excel Discussion (Misc queries) |