Discard unauthorized characters with keypress event

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have used this code to discard all characters except numbers. Now I want to allow the input of points too but I can't make it happen. Can someone fix it for me?


Code:
Private Sub TextBox1_KeyPress (ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Is this what you want?

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9[SIZE=3][COLOR=#ff0000].[/COLOR][/SIZE]]")
End Sub
 
Upvote 0
Is this what you want?

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9[SIZE=3][COLOR=#ff0000].[/COLOR][/SIZE]]")
End Sub


Yes.

One question :

Can we limited the number of points to 1?

This accepts more than one of the points.

Thanks
 
Upvote 0
To allow only ONE point (period) ( . ) try...

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        Case Asc("."): If UBound(Split(TextBox1.Text, ".")) > 0 Then KeyAscii = 0
        Case Else: KeyAscii = 0
    End Select
End Sub
 
Last edited:
Upvote 0
Taking things a little further
... to limit the number of characters AFTER the point to 2
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    With TextBox1
        If InStr(.Text, ".") > 0 Then
            If Len(Split(.Text, ".")(1)) = [COLOR=#ff0000]2[/COLOR] Then KeyAscii = 0
        End If
        Select Case KeyAscii
            Case Asc("0") To Asc("9")
            Case Asc("."): If UBound(Split(.Text, ".")) = [B][COLOR=#006400]1[/COLOR][/B] Then KeyAscii = 0
            Case Else: KeyAscii = 0
        End Select
    End With
End Sub

To make the logic of the original code more obvious , I amended this line
Code:
Case Asc("."): If UBound(Split(.Text, ".")) [B][COLOR=#006400]> 0[/COLOR][/B] Then KeyAscii = 0
To
Code:
Case Asc("."): If UBound(Split(.Text, ".")) [B][COLOR=#006400]= 1[/COLOR][/B] Then KeyAscii = 0
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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