Userform textbox... change....

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have a form that has 5 text boxes. The calculation is working fine but when I erase my numbers to nothing in textbox 2, I get a "run time error 11" and states "division by zero". Anyone help me out here?

Code:
Private Sub TextBox2_Change()
TextBox5 = Val(TextBox1) / Val(TextBox2) * Val(TextBox3)
TextBox4 = Val(TextBox3) - Val(TextBox5)
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,

It looks like your 5 textbox should accept numerical only and your textbox2 can not =0 because textbox5 is textbox1 divided by textbox2. By definition dividing by 0 is an error.

What you can do is rather prevent textbox2 to equal 0

Code:
[COLOR=#00007f][COLOR=#007f00]'Allow only numbers (no blanks)[/COLOR]
  [COLOR=#00007f]If[/COLOR] [COLOR=#00007f]Not[/COLOR] IsNumeric(TextBox2.Value) And Len(TextBox1) <> 0 [COLOR=#00007f]Then[/COLOR]
    [COLOR=#00007f]If[/COLOR] Len(TextBox1) < 1 [COLOR=#00007f]Then[/COLOR] RemoveChar = 0
    TextBox2 = Left(TextBox2, Len(TextBox2) - RemoveChar)
  [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR][/COLOR]
,
or transform it into 1 when empty (I guess you want to devide by one)

Code:
[COLOR=#00007f]If[/COLOR] TextBox2 = "" [COLOR=#00007f]Then[/COLOR] TextBox2 = 1
end if

if it was me, I would rather pop-up an error message through a userform to ask user to set up a value for textbox2.

If you did not, you should make sure only numerical numbers as input for all your boxes.
 
Last edited:
Upvote 0
ok thanks for the help. I'll play around with each to see which one I like better.
 
Upvote 0
So is the code suppose to look like this when finished? If so it is not working. I've tried several different ways and I am still getting that error.

Code:
If Not IsNumeric(TextBox2.Value) And Len(TextBox2) <> 0 Then
    If Len(TextBox2) < 1 Then RemoveChar = 0
    TextBox2 = Left(TextBox2, Len(TextBox2) - RemoveChar)
  End If
  
TextBox5 = Val(TextBox1) / Val(TextBox2) * Val(TextBox3)
TextBox4 = Val(TextBox3) - Val(TextBox5)
 
Upvote 0
Kamolga,
well I just kept trying different things and I finally got something to work the way I wanted it to. I'm thinking what I have may end up a little buggy. So if you still want to help me out with your code I would appreciate it. At least I got something to get me by for now.

Code:
Private Sub TextBox3_Change()
Dim Calc1 As Long

If TextBox3.Value > "0.001" Then
TextBox5 = Val(TextBox1) / Val(TextBox2) * Val(TextBox3)
TextBox4 = Val(TextBox3) - Val(TextBox5)
Else
Exit Sub
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,027
Members
448,543
Latest member
MartinLarkin

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