TextBox Only Numbers VBA

GeorgeWhite

New Member
Joined
Apr 20, 2017
Messages
27
Hi All,

I currently use this code to only allow users to type numbers into the text box on my userform, I need some help with how I can modify this so the users are also blocked from putting zeros at the beginning of there data?

Code:
Private Sub OnlyNumbers()
    If TypeName(Me.ActiveControl) = "TextBox" Then
    
        With Me.ActiveControl
        
    If Not IsNumeric(.Value) And .Value <> vbNullString Then
    
    MsgBox "Sorry, only numbers allowed", vbInformation
        
        .Value = vbNullString
        End If
        
        End With
        
        End If


End Sub

Many thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about
Code:
Private Sub OnlyNumbers()
   If TypeName(Me.ActiveControl) = "TextBox" Then
   
      With Me.ActiveControl
        [COLOR=#ff0000] .Value = CDbl(.Value)[/COLOR]
         If Not IsNumeric(.Value) And .Value <> vbNullString Then
         
            MsgBox "Sorry, only numbers allowed", vbInformation
            
            .Value = vbNullString
         End If
        
      End With
   
   End If

End Sub
Have a look here for a word of warning about IsNumeric
http://www.excelfox.com/forum/showt...sing-VBA-s-IsNumeric-Function-Read-this-first
 
Upvote 0
Another Option:-
Code:
Private [COLOR="Navy"]Sub[/COLOR] TextBox1_KeyPress(ByVal KeyAscii [COLOR="Navy"]As[/COLOR] MSForms.ReturnInteger)
    [COLOR="Navy"]If[/COLOR] Len(TextBox1) = 0 [COLOR="Navy"]Then[/COLOR]
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[1-9]")
    [COLOR="Navy"]Else[/COLOR]
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
This works great thanks!!

Another Option:-
Code:
Private [COLOR=Navy]Sub[/COLOR] TextBox1_KeyPress(ByVal KeyAscii [COLOR=Navy]As[/COLOR] MSForms.ReturnInteger)
    [COLOR=Navy]If[/COLOR] Len(TextBox1) = 0 [COLOR=Navy]Then[/COLOR]
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[1-9]")
    [COLOR=Navy]Else[/COLOR]
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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