Pass by val, variable passing in VBA

Te4t0n

New Member
Joined
Jan 23, 2005
Messages
21
hi, i have multipule text box's on a form, and then in a macro this code



Code:
Dim ch As String
    ch = Chr$(KeyAscii)
    If Not (ch >= "0" And ch <= "9") Then
        ' Cancel the character.
        KeyAscii = 0
    End If

i wish to send the keyascii to this macro sub procedure from the textbox's onkeypress event, how is this done in VBA ? thank you
 

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).
Simpler code in userform module:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

or if you want to branch ,code in userform module:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = passv(KeyAscii)
End Sub

put this in the general module:

Function passv(ByVal n As Long)
If n < 48 Or n > 57 Then
passv = 0
Else
passv = n
End If
End Function
 
Upvote 0
Hi Thomas,

How about something like this (it only accepts lowercase letters but you can amend it to suit your requirements):
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = CheckKey(KeyAscii)
End Sub

Function CheckKey(ByVal KeyAscii As Integer) As Integer
    
    CheckKey = KeyAscii
    'set default value
    
    Select Case KeyAscii
        Case Is < 48: CheckKey = 0
        Case 48 To 57:  CheckKey = 0 'numbers 0 to 9
        Case 58 To 64: CheckKey = 0
        Case 65 To 90:  CheckKey = 0 'capital letters
        Case 91 To 96: CheckKey = 0
        Case 97 To 122: 'lowercase letters
        Case Else: CheckKey = 0
    End Select
    'amend CheckKey value by reference to KeyAscii value
    
End Function
HTH

EDIT : Chito was TDQ!
 
Upvote 0

Forum statistics

Threads
1,218,746
Messages
6,144,262
Members
450,533
Latest member
xoxo1998

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