ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   error in userform (https://www.excelbanter.com/excel-discussion-misc-queries/84669-error-userform.html)

damorrison

error in userform
 
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?


Bob Phillips

error in userform
 
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?




Dave Peterson

error in userform
 
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

error in userform
 
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

damorrison

error in userform
 
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

error in userform
 
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

damorrison

error in userform
 
I didn't notice that, either
Thanks



All times are GMT +1. The time now is 08:24 PM.

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