Formatting numbers in a userform textbox

newbieX

New Member
Joined
Jul 24, 2013
Messages
35
I have the following code behind a textbox. I expect it to complain if the number enter is greater than 999.99, yet I can add 345566 and it gets accepted. What am I missing?

Code:
Private Sub EntryBox12_AfterUpdate() 'Start Mile Marker
If EntryBox12.value <> "" Then
    With EntryBox12
        If IsNumeric(.Text) = False Then
            .Text = "Enter Number"
            MsgBox "Enter a number.", vbExclamation
            Exit Sub
        Else
            .value = Format(EntryBox12.value, "##0.0")
        End If
    End With
End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello. I found this thread while searching for something similar. Maybe it will help you out

https://www.mrexcel.com/forum/excel-questions/395389-restrict-textbox-values-userform.html


My code restricts the textbox to just numbers but I am of the understanding that the code:

Code:
.value = Format(EntryBox12.value, "##0.0")

is stating that entry can only be a maximum of 4 digits long. 3 numbers before the decimal place and 1 after it. Thus 123.4, 12.3. 1.2, 0.1 are all acceptable but 1234.5 is not. However on the userform I can enter 1234.5 without complaint. What am I misunderstanding about the format code?
 
Upvote 0
If you want to restrict the length of the entry, you could do this:

Code:
If Len(EntryBox12.Value)>5 Then
     MsgBox "Please enter a valid number"
     Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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