vba not containing number

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
How do I restrict a textbox on a form so that the user cannot enter a number into the field.

I tried using isnumeric which doesn't allow me to input "88" for instance but would allow "J88".

Would I have to use Keyascii to disallow vb0 to vb9 or something along those lines?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You want to stop him entering any numeric digits at all - yes?

You could loop along the string For 1 To Len(TextBox) using IsNumeric on each character, and if any of them is numeric, issue an error message and make him re-enter it.

Is he allowed to enter £$%^&*?
 
Upvote 0
sorry for the late reply. Have been away.

Thanks for your help. In answer to your question, characters such as £$%^& would ideally not be allowed either.

It's for a Name value, so the only possible exception on that which I can think of would perhaps be a " - " when double barrel names are concerned.
 
Upvote 0
How do I make it so that the code looks at each character individually?

I understand the To Len(textbox) procedure, but how do I specify from the first character?

Left(textbox,1) ?
 
Upvote 0
So I haven't used the loop function, but I have banned numeric value by using this code

Code:
Private Sub Textbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
KeyAscii = 0
Case Else
End Select
End Sub

is there a way to use the same process to restrict the user from entering !"£$%^ etc, without typing each one individually.

Also is there a way to convert the first character after a "-" to uppercase?

I have used this code to convert the first character and after a " " to convert to uppercase, but need it to do the same for double barrel names seperated by a "-"

Code:
Private Sub Textbox_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
txtName.Value = StrConv(txtName, vbProperCase)
End Sub
 
Last edited:
Upvote 0
Rather than disallow invalid characters, I would probably allow valid ones and disallow everything else:-
Code:
Private Sub Textbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case Asc("A") To Asc("Z"), Asc("a") To Asc("z"), Asc("'"), Asc("-"), Asc(" ")
[COLOR=green]      ' alphabetic, apostrophe, hyphen or space - all ok
[/COLOR]  Case Else
[COLOR=seagreen]    [/COLOR][COLOR=green]  ' anything else not ok!
[/COLOR]    KeyAscii = 0
    End Select
End Sub
 
Upvote 0
I have used this code to convert the first character and after a " " to convert to uppercase, but need it to do the same for double barrel names seperated by a "-"

Code:
Private Sub Textbox_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
txtName.Value = StrConv(txtName, vbProperCase)
End Sub

Does it not do that? I would have thought it did.
 
Upvote 0
Then this:-
Code:
txtName =WorksheetFunction.Proper(txtName)
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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