Solved

Validating a string in an Excel userform

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

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running Access application from Task Scheduler 6 36
What is format f12.8 for a CSV file 6 41
Excel printing page management 2 25
Access 2010 Query Syntax 5 25
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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…

861 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

30 Experts available now in Live!

Get 1:1 Help Now