Solved

Validating a string in an Excel userform

Posted on 2013-01-04
4
335 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 39

Accepted Solution

by:
als315 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:als315
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now