Share via

V W X Y Z v w x y z : only these letters are not being selected correctly using Select Case function in VBA.

Philipp Borer 0 Reputation points
2026-02-02T20:10:30.31+00:00

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
Microsoft 365 and Office | Development | Office JavaScript API
{count} votes

Answer recommended by moderator
  1. Philipp Borer 0 Reputation points
    2026-02-04T18:21:32.8866667+00:00

    Resolved.

    Thank you all for your answers, the original code works and there was some other code interfering after the fact.


3 additional answers

Sort by: Most helpful
  1. Anthony Lee 1,145 Reputation points
    2026-02-04T19:02:45.2933333+00:00

    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.

    0 comments No comments

  2. Flora-T 11,365 Reputation points Microsoft External Staff Moderator
    2026-02-03T05:58:05.7966667+00:00

    Hi Philipp Borer

    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.


  3. Viorel 126.7K Reputation points
    2026-02-03T03:41:48.87+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.