If Code for limits

Goody61865

Board Regular
Joined
Mar 4, 2011
Messages
50
I am trying to write a code for a textbox to not allow a number to be entered that is outside of the range .105 - .125, but for some reason this is kicking my butt. Is this really that difficult?

Can someone help me?

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I'm doing this from memory, and I don't recall the proper declaration of the TextBox1_BeforeUpdate sub, but something like this should work.

Code:
Sub TextBox1_BeforeUpdate (Cancel as Boolean): Rem replace this line
    Cancel = (Val(Textbox1.Text) < .105) Or (.125 < Val(TextBox1.Text))
Exit Sub
 
Upvote 0
Now that I'm back at my Excel, this is the proper syntax. You may need to alter the name of the textbox to suit your situation.
Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Cancel = (Val(TextBox1.Text) < 0.105) Or (0.125 < Val(TextBox1.Text))
    If Cancel Then Beep
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,767
Members
452,940
Latest member
rootytrip

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