Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default restricting input characters

As part of a macro, I open an input box. Here is the part I'm not sure how
to code: I'd like the input to be restricted to either 1 or 2 alphanumeric
characters ONLY. (no spaces, other symbols, =3 characters, etc.)

1, Ab, 4G, are all OK.
#4, 1 2, A3b are not OK.

Could someone offer a few clues?

Thanks
Bri






  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default restricting input characters

I wouldn't use an input box - use a user form, which has keypress
events - which means you can restrict the key's being pressed by code.
You can also restrict the input length to the two characters you want.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default restricting input characters

ok, good suggestion, but I still am not sure how to code that. any clues"
Bri


wrote in message
oups.com...
I wouldn't use an input box - use a user form, which has keypress
events - which means you can restrict the key's being pressed by code.
You can also restrict the input length to the two characters you want.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default restricting input characters

assuming to use input box, i'm not sure this one would satisfy you. but
try this.

Sub testme()
Dim strprompt As String
Dim flg As Boolean
Dim sdata As String
Dim res

strprompt = "input only alphanumeric"
flg = True
sdata = "0123456789abcdefghijklmnopqrstuvwxyz"

Do While flg
res = Application.InputBox(strprompt, _
Default:=res, Type:=3)
If VarType(res) = vbBoolean Then
Exit Sub
ElseIf Len(res) = 1 Then
If InStr(sdata, Left(LCase(res), 1)) _
0 Then

flg = False
Else
strprompt = _
"Wrong Data!! Not alphanumeric"
End If
ElseIf Len(res) = 2 Then
If InStr(sdata, Left(LCase(res), 1)) 0 _
And InStr(sdata, Right(LCase(res), 1)) _
0 Then

strprompt = "Wrong Data!!"
flg = False
Else
strprompt = _
"Wrong Data!! Not alphanumeric"
End If
Else
strprompt = _
"Wrong Data!! Too many characters"
End If
Loop
MsgBox res
End Sub

"Bri" wrote in message
...
As part of a macro, I open an input box. Here is the part I'm not

sure how
to code: I'd like the input to be restricted to either 1 or 2

alphanumeric
characters ONLY. (no spaces, other symbols, =3 characters, etc.)

1, Ab, 4G, are all OK.
#4, 1 2, A3b are not OK.

Could someone offer a few clues?

Thanks
Bri







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default restricting input characters

Insert a user form into your module

Insert a text box - use the properties to set maxlength at 2

in code use

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii 122 Then KeyAscii = 0
If KeyAscii 57 And KeyAscii < 65 Then KeyAscii = 0
If KeyAscii 90 And KeyAscii < 97 Then KeyAscii = 0
End Sub


This will allow upper and lower case and numbers ONLY, and a maximum
length of 2



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restricting Number of characters in a cell Colin Hayes Excel Worksheet Functions 9 September 11th 09 01:45 PM
Restricting Characters within a User Form DarnTootn Excel Discussion (Misc queries) 3 December 10th 08 10:45 PM
restricting use of certain characters Rob Excel Discussion (Misc queries) 4 June 8th 06 01:07 AM
Restricting Characters in a cell. R Bitterman Excel Programming 3 October 13th 05 02:47 PM
Restricting User Input... Darin Kramer Excel Programming 2 February 4th 05 04:52 PM


All times are GMT +1. The time now is 06:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"