Code and Functions

k209310

Active Member
Joined
Aug 12, 2002
Messages
382
Hello

I created some ASCII code that only allows a backslash (\) and numeric chartacters to be entered in to a text box. The code works fine if its in the keypress event of a textbox.

However if i create the code in a module as a public function and try to call it from the key press function then the code fails (all spellings are correct) althought the function works when tested with other code.

Does anybody have any idea why i cant create a function to delete ascii characters.

This isnt a huge a problem but it will be easier to create a function than to put the code in to every keypreess event where it is needed. The code is below

Select Case KeyAscii
Case Is < 46
KeyAscii = 0

Case Is > 57
KeyAscii = 0

End Select
 

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 have tried
Code:
Public Function NoAlpha(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Is < 46
KeyAscii = 0

Case Is > 57
KeyAscii = 0

End Select
End Function

this first method produces the 'argument not optional error message'

and

Code:
Public Function NoAlpha()
Select Case KeyAscii
Case Is < 46
KeyAscii = 0

Case Is > 57
KeyAscii = 0

End Select
End Function

This second method doesnt work but produces no error message.

Im simply call the Noalpha from the Keypress event as it appears when slected
 
Upvote 0
Hi,

Try this:

In the UserForm code (for TextBox1):
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = Module1.NoAlpha(KeyAscii)
End Sub
And in a general module (Module1 in this example):
Code:
Public Function NoAlpha(ByVal KeyAscii As Integer) As Integer
    
    NoAlpha = KeyAscii
    If KeyAscii = 8 Then Exit Function
    'accept by default and allow delete
    
    Select Case KeyAscii
        Case Is < 46: NoAlpha = 0
        Case Is > 57: NoAlpha = 0
    End Select
    'restrict acceptable input
    
End Function
HTH
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,336
Members
451,697
Latest member
pedroDH

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