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

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Checking characters

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 112
Default Checking characters

Thank you Jakob,
it worked fine!
Cheers
Mats

"Jacob Skaria" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Checking characters

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" wrote in message
...
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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 112
Default Checking characters

Thank you Rick,
that works well too.
It's no big deal in my case. The value picked to the textbox is a customer
order number that will be used later on in the filename for reference. In
most cases the invalid character is a : or a /. Will keep your solutions in
mind, new things happens
all the time!
Regards
Mats

"Rick Rothstein" wrote:

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" wrote in message
...
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



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
Checking characters , non-printable etc. dhstein Excel Discussion (Misc queries) 1 February 25th 10 11:57 PM
Checking for special characters in a string John Excel Programming 1 February 4th 09 08:37 PM
Checking a cell for only Alphanumerical characters Trefor Excel Worksheet Functions 2 August 3rd 07 10:38 AM
Checking for Invalid filename characters John Keith[_2_] Excel Programming 2 May 8th 07 08:31 PM
Checking characters using VBA Gary[_5_] Excel Programming 1 August 4th 03 05:57 PM


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

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

About Us

"It's about Microsoft Excel"