Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
    hasArabic = False
    Dim i As Long
    Dim c As Long
    For i = 1 To Len(str)
        c = AscW(Mid(String:=str, Start:=i, Length:=1))
        If c >= &H600 And c <= &H6FF Then hasArabic = True: Exit For
    Next i
End Function

I have tried to convert the above If Statement to Select Case Statement but the following code doesnt work.

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

So please support me how to convert from If Statement to Select Case Statement.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The microsoft link:

https://learn.microsoft.com/tr-tr/office/vba/language/reference/user-interface-help/select-case-statement?f1url=%3FappId%3DDev11IDEF1%26l%3Dtr-TR%26k%3Dk(vblr6.chm1008810)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv15)%26rd%3Dtrue

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading