# Userform textbox... change....

#### buzz71023

##### Active Member
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use \$ signs: \$V\$2:\$Z\$99 will always point to V2:Z99, even after copying
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:
ok thanks for the help. I'll play around with each to see which one I like better.

Good luck, just noticed I left textbox1 instead of textbox2 in first code

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)``````

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``````

Replies
9
Views
385
Replies
3
Views
166
Replies
4
Views
276
Replies
13
Views
309
Replies
0
Views
51

1,203,435
Messages
6,055,366
Members
444,781
Latest member
rishivar

### 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.

### Which adblocker are you using?

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

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