Limiting decimal place

Takes2ToTango

Board Regular
Joined
May 23, 2023
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Is there a way to limit decimal places? For example, I want to be able to write as many numbers before the '.' but as soon as the decimal is inserted, I want to limit it to 4 numbers afterward. So for example - 33.1234. Once 4 is inserted, then the user can no longer input any numbers.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,

I don't believe you can stop a user typing infinite digits into any cell, but you could use a popup box or the like to ask them to enter it, and then act on that to store it in some place.

Also, a good tool could be this as a formula :
Excel Formula:
=TRUNC(number,[num_digits])

As you can specify how many digits you want your formula to use from that cell (eg. it might have .2345678 after your point, but if you use [4] in your number of digits, it will only use those..

Any use ?
Rob
 
Upvote 0
Hi Rob,

Sorry, i forgot to add that this is for a userform textbox! Can you use that same code for this Vba textbox?
 
Upvote 0
Hi,

well thats a little different, but perhaps better for you. There is no Trunc() in VBA sadly.

So my thought would be to use something like INSTR() to search for the "." , then use the left hand part of the string up to the dot, and add the right hand 4 chars after the "." to it by using concatenate() or a simple "&" ?

Rob
 
Upvote 0
as an example :
VBA Code:
Sub test()

Dim textval As String

textval = InputBox("data")
pt_pos = InStr(1, textval, ".")

If pt_pos > 0 Then

    result = Int(textval) & Mid(textval, pt_pos, Len(textval) - pt_pos)

End If



End Sub
 
Upvote 0
Hi All,

Many thanks for you replies. Joe, that link you provided had the answer!
Rob, many thanks - I used your code with the text input but the input didn't seem to populate the main text box
 
Upvote 0
No problem, as long as you found your answer.

My code just stored the answer in "Result" after you typed it in - so then its up to you what to do with it / where to put it ..

Thanks for feedback

Rob
 
Upvote 0
No problem, as long as you found your answer.

My code just stored the answer in "Result" after you typed it in - so then its up to you what to do with it / where to put it ..

Thanks for feedback

Rob
That could be why I couldn't see it. I don't think I told it where to 'add' it too
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,234
Members
449,092
Latest member
SCleaveland

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