View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson[_3_] Dave Peterson[_3_] is offline
external usenet poster
 
Posts: 2,824
Default preventing input of illegal characters in an inputbox

Another option is to just try to use the name and look for an error:

Option Explicit
Sub testme01()

Dim myStr As String
Dim myErr As Long

Do
myStr = InputBox("enter a string")
If Trim(myStr) = "" Then
'user cancelled
Exit Sub
Else
On Error Resume Next
ActiveSheet.Range("a1").Name = myStr
myErr = Err.Number
Err.Clear
On Error GoTo 0

If myErr < 0 Then
'an error occurred
MsgBox "try again"
Else
Exit Do
End If
End If
Loop

End Sub



drofnats wrote:

Still a little new at vba. Can someone tell me how to
prevent someone from entering in spaces or any illegal
characters (like & % @ etc) into an InputBox? At least
without writing a whole lot of "ElseIf" statements?

I have my script so it so names the range as whatever they
type. That's why I need to restrict entries. Thanks.


--

Dave Peterson