Solved

Validating a string in an Excel userform

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

813 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

18 Experts available now in Live!

Get 1:1 Help Now