473,396 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Restrict User Input from inputting non-ASCII characters

How do I restrict non-ASCII keystroke inputs on all cells of a worksheet. The cell should only accept ASCII characters.

Is this even possible?

Supposedly, "KeyPress" event can do the trick, however "KeyPress" is only for objects. Selecting worksheet doesnt have the "KeyPress" event.

This is a sample.

---------
|I | (initial)
---------
|AI | (user type "A")
---------
|AbI | (user type "b")
---------
|AbcI | (user type "c")
---------
|AbcI | (user type "@" but rejected)
---------
|AbcI | (user type "@" but rejected)
---------
|Abc1I | (user type "1")
---------
|Abc12I | (user type "2")
---------
Oct 11 '13 #1
9 2705
ADezii
8,834 Expert 8TB
I am not sure if you can capture 'individual keystrokes' in a Cell, but once a Value is written within a Cell you can easily remove any unwanted keystrokes and return the filtered value. The following Code, in the Worksheet's Change() Event will analyze the Values in a Cell once the User has left it, and return only Capital Letters between A-Z:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim varCellValue As Variant
  3. Dim intLen As Integer
  4. Dim intCharCtr As Integer
  5. Dim strBuild As String
  6. Dim rngRange As Excel.Range
  7.  
  8. Set rngRange = Target
  9.  
  10. varCellValue = Target.Value
  11.  
  12. If IsNull(varCellValue) Then Exit Sub
  13.  
  14. intLen = Len(varCellValue)
  15.  
  16. For intCharCtr = 1 To intLen
  17.   Select Case Asc(Mid(varCellValue, intCharCtr, 1))
  18.     'Only allow Capital Letters A to Z
  19.     Case 65 To 90
  20.       strBuild = strBuild & Mid(varCellValue, intCharCtr, 1)
  21.     Case Else       'do nothing
  22.   End Select
  23. Next
  24.  
  25. rngRange = strBuild
  26. End Sub
Oct 11 '13 #2
NeoPa
32,556 Expert Mod 16PB
As far as I can tell (and I checked so I'm pretty confident of this ;-)) there is no facility within Excel to capture key strokes within either a workbook or a worksheet.

A couple of things to bear in mind re ADezii's code :
  1. The Target parameter is an Excel.Range object. They can take both extended ranges (A5:C12) as well as multiple ranges in a list (A5:C12,P7,R3:S7). The code doesn't currently handle that, but it could be extended and does illustrate the concept you would need to use to handle your data input.
  2. The Select Case on line #17 needn't include the Asc() part as long as line #19 says Case "A" To "Z"
    This also makes the code clearer.
The code is there as an illustration of what can be done. Equally, you could replace each unacceptable character with another instead if you preferred, an asterisk (*) for instance, simply by putting that in the Case Else section.
Oct 13 '13 #3
zmbd
5,501 Expert Mod 4TB
which version of Excel/Office and what OS are you working with?
Also... all characters from dec000 thru dec255 are recognized under the extended ASCII guidelines. You need to be more specfic about the range:
http://www.ascii-code.com/
We're only assuming you mean to restrict between dec32 and dec127
Oct 13 '13 #4
ADezii
8,834 Expert 8TB
  1. The Range Object to which you are referring represents a single Cell. Once a Value is entered into any Cell, the Change() Event is fired. I don't see how a Range, outside of a single Cell, is relevant here.
  2. The key point in this Thread is the restricting of Non-ASCII characters, some of which have no visible representation. In my opinion, you have to deal with the ASCII characters themselves (65 - 90) as opposed to what they represent (A - Z).
  3. Of course, I could completely be off in my interpretation of the Question (LOL).
Oct 13 '13 #5
zmbd
5,501 Expert Mod 4TB
@ADezii: I think you're pretty much on with the intrepretation you have made.

It's just that I am a very oldschool programmer (think punch-cards and COBAL and a teletype terminal --- ah the days of 1K programming (@.@)) and, as you have mentioned, the term ASCII refers to a very large amount of information.

@optimus

IMHO, you need to tell us the true nature of the question.

However, using the same inference that ADezii made and the examples shown, I suspect that you are asking to restrict the entry to the alphanumeric range.

There is a way to do this using data-validation and a custom function; however, you need to do two things:
1) Tell us which version of Excell/Office is being used
2) Confirm the ASCII span to the alphanumeric, or any printable character, etc... Because, as written, you are mis-using the ASCII term - hence why I provided the link to the table of ASCII values.

Programming terminology is like Chemistry... the details are importaint and when misused may result in very unintended results!
Oct 13 '13 #6
NeoPa
32,556 Expert Mod 16PB
@ADezii.

#1 - Not true, I'm afraid ADezii. Try the following :
  1. Select the range I specified (A5:C12,P7,R3:S7).
  2. Enter the keystroke "X".
  3. Use Ctrl-Enter to enter that value into each of the selected cells.
You'll see that Target doesn't refer to a single cell.

A more common situation is found when pasting blocks of cells.

#2 - While the wording of the question indeed states ACSII characters, the example shows that the wording is not correct, and even a little misleading. The example indicates that the OP needs alphabetic characters. In fact, a point I missed earlier, it doesn't even indicate they must be upper-case.

That said, I find it clearer to use the characters themselves in the code. This seems markedly clearer to my mind as to exactly what is being achieved. Nevertheless, if you see this as relating to the ASCII code set in some way (other than randomly), then I see where you're coming from, even if I don't believe it makes good sense from my perspective. It works as an illustration of the concept anyway - and that's the most important thing.

#3 - Interpreting questions is often extremely difficult. In this case we're only talking minor details. Worth bringing up for completeness, and for expanding the understanding of any readers, but certainly not to be seen as criticism. When the details of a question need to be inferred rather than simply read, it makes it all the more difficult to get them all right every time.

@Optimus.

You should not take this discussion of the flaws in your question as criticism either. It's a first post and what we're talking about is not something everyone would even be aware of. Some of us have been dealing with such details for many years and can be expected to know the ins and outs in more detail. That level of expectation would not cover a member of the general public.
Oct 13 '13 #7
zmbd
5,501 Expert Mod 4TB
Neopa: "Use Ctrl-Enter to enter that value into each of the selected cells"
I forget about those silly array entries too... think that I wouldn't though given how often I use the statistical array formulas!

Neopa: @Optimus.
You should not take this discussion of the flaws in your question as criticism (...)
Here-Here and a second on that comment Optimus!
Rarely should any comment here be taken as anything more than "constructive" in intent with the goal to either clarify the goal of the question or provide some insight and point to a better way of doing something. (in fact, I had to learn this myself (^-^) when I started posting here and one of the reasons I keep coming back!)
Oct 13 '13 #8
ADezii
8,834 Expert 8TB
@NeoPa:
One of us is missing the point, and it is probably me (LOL). If you duplicate your steps exactly: selecting a Range, entering a Value within the Range, then CTRL+ENTER to copy that Value to the other Cells within that Range, the Change() Event will fire but will only evaluate and return the proper result in the Cell containing the original Value. This is what I meant by Target referring to only a single Cell. Make sense, or not?
Oct 14 '13 #9
NeoPa
32,556 Expert Mod 16PB
It seems you may not be following my instructions correctly ADezii.

If you create a new workbook and put the following code behind "Sheet1" :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub Worksheet_Change(ByVal Target As Range)
  4.     Call MsgBox(Target.Address)
  5. End Sub
Now follow the instructions exactly as stipulated. The "X" is NOT entered in step #2, it is simply keyed and left until step #3 to enter in any cells.

You should see a message box with the following :
Expand|Select|Wrap|Line Numbers
  1. $A$5:$C$12,$P$7,$R$3:$S$7
Alternatively, try copying and pasting a block of cells into that worksheet. Again, you'll see that Target is not a single cell. The more complicated scenario was simply to illustrate the full range of possibilities for the Target parameter.
Oct 15 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
1
by: CAD Fiend | last post by:
Hello, I am making a form that will contain address fields for a land owner, but sometimes the land owner does not live on the same property they own (for instance, they may be renting it out to...
10
by: sconeek | last post by:
hi all, i have a textfield where i would like the user to input only Y or N. can somebody tell me how can i restrict the user from entering any other character, number or special character....
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
2
by: noosaj | last post by:
Hi, I'm designing a basic sales tax calculator. I'm in a bind here. I am trying to figure out how to prevent the user from inputting anything other than numbers in the textboxes. Obviously,...
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
12
by: Tarique | last post by:
I have tried to restrict the no. of columns in a line oriented user input.Can anyone please point out potential flaws in this method? btw.. 1.I have not used dynamic memory allocation because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.