An Office service that supports add-ins to interact with objects in Office client applications.
Resolved.
Thank you all for your answers, the original code works and there was some other code interfering after the fact.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
V W X Y Z v w x y z : only these letters are not being selected correctly using Select Case function in VBA.
not working for select case, using either the "_" or the ascii code, upper or lower case. Works for every other letter and number. code returns correct ascii codes for all letters in the cell.
Function AlphaNumericOnly(strSource As String) As String
'takes special characters out of the string when called (folder name)
Dim i As Integer
Dim strResult As String
For i = 1 To Len(strSource)
Select Case Mid(strSource, i, 1)
'comment: Case 1 To 31, 33 To 47, 58 To 64, 91 To 96, 123 To 126
Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 118 To 122, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 87 To 90
'http://www.asciitable.com/ to view ascii codes that you want to allow
strResult = strResult & Mid(strSource, i, 1)
End Select
An Office service that supports add-ins to interact with objects in Office client applications.
Resolved.
Thank you all for your answers, the original code works and there was some other code interfering after the fact.
This is the cleanest and closest to your original logic:
Select Case Asc(Mid(strSource, i, 1))
Case 48 To 57, 65 To 90, 97 To 122 '0–9, A–Z, a–z
strResult = strResult & Mid(strSource, i, 1)
End Select
This works perfectly for all letters including V–Z and v–z.
Thank you for reaching out to Microsoft Q&A Forum and sharing your experience.
I reviewed your snippet and also reproduced this locally. Your uppercase ASCII range is 87 To 90, which matches W–Z only. V is ASCII 86, so it will never match that range. Changing it to 86 To 90 would include V. Also, your Select Case expression is a String (Mid(...)), but the statement mixes string cases ("a", "B", ...) with numeric ranges (118 To 122, 87 To 90). Numeric ranges only behave predictably when the Select Case expression is numeric.
To make the comparison consistent and avoid edge-case matching issues, converting the character to its Unicode value and comparing numeric ranges is the most reliable approach. I tested the following pattern and it correctly keeps all letters including V–Z and v–z, plus digits:
Function AlphaNumericOnly_FIXED(ByVal strSource As String) As String
Dim i As Long
Dim ch As String
Dim code As Long
Dim strResult As String
For i = 1 To Len(strSource)
ch = Mid$(strSource, i, 1)
code = AscW(ch)
Select Case code
Case 48 To 57, 65 To 90, 97 To 122 '0-9, A-Z, a-z
strResult = strResult & ch
End Select
Next i
AlphaNumericOnly_FIXED = strResult
End Function
Please let me know how things go on your side as your update can also assist others in the community who may be facing the same issue.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
It seems that characters and codes cannot be mixed.
To use codes:
Select Case AscW(Mid(strSource, i, 1))
Case 48 To 57, _
65 To 90, _
97 To 122
strResult = strResult & Mid(strSource, i, 1)
End Select
To use characters:
Select Case Mid(strSource, i, 1)
Case "1" To "9", _
"A" To "Z", _
"a" To "z"
strResult = strResult & Mid(strSource, i, 1)
End Select
Perhaps Regular Expressions or other methods can be used too.