Description:
|
BadChar tests for illegal character in file names, directory names, xlsheet names, etc. If an illegal character is encountered, the function returns the string index value for the illegal character. If no illegal characters are found, BadChar returns zero.
|
Discussion:
|
WinXX and MS applications do not allow certain characters to be embedded in file names, directory names, Excel sheet names, etc. When creating such names under VBA control, one can do an error test to detect invalid names, but a better approach is to test the text for invalid characters before using the name.
BadChar is a function of type Long that tests and text string for invalid or bad characters, i.e., " :? , ?\? , ?< " , "/" , "?" , "*" , "[" , "]".
BadChar can be used in any VBA application. The demo is Excel-based.
|
Option Explicit
Function BadChar(strText As String) As Long
Dim BadChars As String
Dim I As Long
Dim J As Long
BadChars = ":\/?*[]"
For I = 1 To Len(BadChars)
J = InStr(strText, Mid(BadChars, I, 1))
If J > 0 Then
BadChar = J
Exit Function
End If
Next I
BadChar = 0
End Function
|