understanding a code

brodaddy2002

Board Regular
Joined
Jan 21, 2013
Messages
67
I found this code on the internet, but cannot find it again. My question is, what does the numbers 8 and 0 mean? What do they do? Does KeyAscii = 0 make the value "null"?

Private Sub StrikeAltText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
‘StrikeAltText is the name of the textbox that the user is entering data

Select Case KeyAscii
Case vbKey0 To vbKey9, 8 ‘ I don’t have any idea what the number 8 does.
Case Else
KeyAscii = 0
Beep
End Select

End Sub

Any Help will be greatly appreciated.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I guess another easy softball. What does the comma(,) mean in the program? The select case chooses the numeric values 0 through 9, and then executes a backspace? I completely understand the case else on. I am just not sure about the Case vbKey0 to vbKey9, 8 line.
 
Upvote 0
Copy All. It is starting to become clear. One more quick question. In the top line, we use ByVal KeyAscii As MSForms.ReturnInteger. Do we have to use MSForms.ReturnInteger? Can we simply just say ByVal KeyAscii As Integer?
 
Upvote 0
The piece of code is an event handler. It has a special signature and if you change it then the code won't work - in fact, it's very likely the code won't even compile. If you really wanted to, you could probably get away with removing the MSForms qualifier, leaving you with:
Private Sub StrikeAltText_KeyPress(ByVal KeyAscii As ReturnInteger)

but it's bad practice to do so. ReturnInteger is not the same as Integer: ReturnInteger is actually a class in the MSForms library. It's best just to leave the MSForms. qualifier in there in case you have a reference to another library which also has a class called ReturnInteger (which would cause a clash). Perhaps it will make the fact that MSForms.ReturnInteger is an object (an "object" is an instance of a class) if we use its Value property explicitly?
Code:
Private Sub StrikeAltText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    'StrikeAltText is the name of the textbox that the user is entering data
 
    Select Case KeyAscii.Value
        Case vbKey0 To vbKey9, vbKeyBack
            'OK
        Case Else
            KeyAscii.Value = 0
            Beep
    End Select
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,635
Messages
6,186,128
Members
453,340
Latest member
Stu61

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