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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Chitosunday

Well-known Member
Joined
Jul 14, 2003
Messages
1,017
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
 

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
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!
 

Forum statistics

Threads
1,147,690
Messages
5,742,647
Members
423,746
Latest member
Joaogomes

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
Top