Solved

Validating a string in an Excel userform

Posted on 2013-01-04
4
371 Views
Last Modified: 2013-01-04
I need a visual basic function to validate a string in a textbox and a combobox in an excel userform.  It can have letters, numbers, or several (15 or so) special characters. Not all the special characters, just certain ones. This is what I have so far:

I pass it the string from the _Exit event of the textbox and combobox.

Public Function ValidateFileNameCharacters(ByVal CheckString As String) As Boolean

     Dim sTemp As String
    Dim iLen As Integer
    Dim iCtr As Integer
    Dim sChar As String
   
    ValidateFileNameCharacters = False
    sTemp = CheckString
    If iLen > 0 Then
        For iCtr = 1 To iLen
            sChar = Mid(sTemp, iCtr, 1)
           
            If Not sChar Like "[0-9A-Za-z]" Then
              If Not Asc(sChar) Like "[32,33,36-38,42, 55-60]" Then Exit Function
             End If
              Next
     
       End If
    ValidateFileNameCharacters = True
   End Function

What I don't know how to do is check for the list of special characters -

   If Not Asc(sChar) Like "[32,33,36-38,42, 55-60]" Then

This line isn't working.  Any ideas?

Thanks!
0
Comment
Question by:loybob
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 500 total points
ID: 38744847
You can use one like, because all characters are printable:
            If Not sChar Like "[ (!)$%&(*):;<0-9A-Za-z]" Then Exit Function
There is one more error: iCtr is not assigned to len(sTemp)
More details about special characters you can find here:
http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx
0
 

Author Comment

by:loybob
ID: 38744893
In your example, how does it separate all those special characters and then 0-9   How does it know the dash doesn't mean a dash, it means 0 to 9?
0
 

Author Closing Comment

by:loybob
ID: 38745463
This worked, even without the answer to my followup question.  Thanks so much.
For the iCtr, I accidently left that out when I copied it over - it's in there in the real code!
Thanks again.
0
 
LVL 40

Expert Comment

by:als315
ID: 38746270
There is special paragraph about dash (Hyphen) in link above (it should be placed first or last):
"Usage of the Hyphen
A hyphen (–) can appear either at the beginning (after an exclamation point, if any) or at the end of charlist to match itself. In any other location, the hyphen identifies a range of characters delimited by the characters on either side of the hyphen."
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question