You don't need to loop through all the characters in the TextBox to do this;
you can make use of the Like operator instead...
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox1.Text Like "*[%&/.]*" Then
MsgBox "One of these Invalid character are present: %&/."
Cancel = True
End If
End Sub
However, for a general solution, I think there are too many invalid
characters to list. Using the valid character list at this link...
http://support.microsoft.com/kb/177506
Here is a function that can be called to test if a text string is composed
of valid characters or not...
Function IsValidFileName(FileName As String) As Boolean
IsValidFileName = InStr(FileName, "]") > 0 Or Not FileName _
Like "*[! 0-9A-Za-z^&'@{}[,$=!#()%.+~_-]*"
End Function
The OP can then use this function like this...
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsValidFileName(Me.TextBox1.Text) Then
MsgBox "Invalid character present!"
Cancel = True
End If
End Sub
--
Rick (MVP - Excel)
"Jacob Skaria" <(E-Mail Removed)> wrote in message
news:C55969D1-48DA-41B4-A157-(E-Mail Removed)...
> Try this Textbox1 Exit event...code...You can add more invalid characters
> to
> the variable strInvalid...
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> Dim strInvalid As String, intTemp As Integer
> strInvalid = "%&/."
> For intTemp = 1 To Len(strInvalid)
> If InStr(Me.TextBox1.Text, Mid(strInvalid, intTemp, 1)) > 0 Then
> MsgBox "Invalid character present " & strInvalid
> Cancel = True
> End If
> Next
> End Sub
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Mats Samson" wrote:
>
>> RI've a Userform where a textbox picks its value as a proposal from a
>> worksheet cell when initiating.
>> This cell may contain invalid filename characters like %&/.
>> I want the user alerted so they may change the characters in the textbox
>> before proceeding. The corrected textbox string will be saved in another
>> cell.
>> regards
>> Mats