Search code examples
regexvbaif-statement

How to convert from If Statement to Select Case Statement


The following code is okey.

Sub Macro1()
    Dim str As String
    str = Sheets("Sheet1").Range("A1").Value
    hasArabic (str)
End Sub

Function hasArabic(str As String) As Boolean
    Dim i As Long
    For i = 1 To Len(str)
        If AscW(Mid(str, i, 1)) >= &H600 And AscW(Mid(str, i, 1)) <= &H6FF Then
            hasArabic = True
            Exit Function
        End If
    Next i
End Function

Please support me how to convert from If Statement to Select Case Statement.


Solution

  • Select Case is a poor choice if you only want to check one condition but:

    Function hasArabic(str As String) As Boolean
        Dim i As Long
        For i = 1 To Len(str)
            Select Case AscW(Mid(String:=str, Start:=i, Length:=1))
                Case &H600 To &H6FF
                    hasArabic = True
                    Exit Function
            End Select
        Next i
    End Function