#1   Report Post  
Posted to microsoft.public.excel.misc
damorrison
 
Posts: n/a
Default 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?

  #2   Report Post  
Posted to microsoft.public.excel.misc
Bob Phillips
 
Posts: n/a
Default 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?



  #3   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default 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
  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default 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
  #5   Report Post  
Posted to microsoft.public.excel.misc
damorrison
 
Posts: n/a
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default 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
  #7   Report Post  
Posted to microsoft.public.excel.misc
damorrison
 
Posts: n/a
Default error in userform

I didn't notice that, either
Thanks

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
userform that add data in all w/sheets tkraju via OfficeKB.com Excel Discussion (Misc queries) 0 March 24th 06 04:29 AM
Convert Worksheet to Userform Brian C Excel Discussion (Misc queries) 1 October 3rd 05 08:08 PM
Data Validation Cell - Move to UserForm thom hoyle Excel Worksheet Functions 0 April 28th 05 12:23 AM
Cell Content from UserForm Not Retained D.Parker Excel Discussion (Misc queries) 3 April 27th 05 04:56 PM
How can I run a macro in the background whilst a UserForm is visib cdb Excel Discussion (Misc queries) 3 February 10th 05 06:58 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"