?
Solved

Validating a string in an Excel userform

Posted on 2013-01-04
4
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 2000 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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