If textbox value is less than 0, then display 0 problem

WombatTrax

New Member
Joined
Jul 10, 2012
Messages
10
Hey everyone,
Having an issue with a calculation displaying "0" in a text box on a userform if the value is less than "0".
I have two textboxes that do calculations and show the result in a third textbox. I want the third textbox to show a value of "0" if the the calculated value is less than "0". Any help would be great. I've tried a few different If Then statements, but have had no luck. I just can't seem to figure this out even though it seems like it should be relatively simple. Here is the code for the textboxes so far:

Private Sub TextBox3_Change()
If TextBox3.Value = "" Then Exit Sub
TextBox7.Value = CDbl((TextBox3.Value) - 100) / 20
End Sub

Private Sub TextBox4_Change()
If TextBox4.Value = "" Then Exit Sub
TextBox7.Value = CDbl((TextBox4.Value) / 6 + TextBox7.Value)
End Sub

Private Sub TextBox6_Change()
TextBox6.Value = Format(TextBox6.Value, "###")
End Sub

Private Sub TextBox7_Change()
TextBox7.Value = Format(TextBox7.Value, "##")
End Sub

Thanks for any help you can give me.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You could use Application.Max to make sure that if the result of the calculation is less than zero, then TextBox7.Value is set to 0.

Code:
Private Sub TextBox3_Change()
    With TextBox3
        If .Value = "" Or Not IsNumeric(.Value) Then Exit Sub
        TextBox7.Value = Application.Max(0, (.Value - 100) / 20)
    End With
End Sub

 
Private Sub TextBox4_Change()
    With TextBox4
        If .Value = "" Or Not IsNumeric(.Value) Then Exit Sub
        TextBox7.Value = Application.Max(0, .Value / 6 + TextBox7.Value)
    End With
End Sub

In order to display the zero value, you'll need to change the Format statement to this.

Code:
Private Sub TextBox7_Change()
    TextBox7.Value = Format(TextBox7.Value, "0")
End Sub

What are you trying to do with this statement?
TextBox6.Value = Format(TextBox6.Value, "###")
 
Upvote 0
Perhaps run a check to see if the text is numeric, then run the if-statement using TextBox3.Text instead of TextBox3.Value

To be honest, I've never used "TextBox3.Value" unless I'm setting it to "" in order to clear it. Instead, I always use "TextBox3.Text"
 
Upvote 0
Jerry-
Thanks for the code. It works just as I wanted it to. I figured it would be something simple; my brain just wasn't grasping it. Thanks again.
 
Upvote 0
Thanks Memar-
Jerrys' code does exactly as I wanted it to. I'll keep the ".Text" in mind though for future projects.
 
Upvote 0
FYI, For Textbox Form Objects, .Text and .Value are always the same. So you'll get the same result reading either property.
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,322
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top