Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check string for uppercase characters

Tags:

excel

vba

How do I check each character in a cell to see if it is upper case?

UCASE converts letters to uppercase but I want to check.

I'm not sure where the "do" statement should go.

Function Italic(rng As Range) As Variant
    Dim strng As String
    Dim iEnd As Long, iIni As Long, strngLen As Long

    strngLen = Len(rng.Value2)
    iIni = 1

    Do While iEnd <= strngLen
       Do While UCase(Mid(rng.Value, iEnd, 1)) = Mid(rng.Value, iEnd, 1) And Not rng.Characters(iEnd, 1).Font.Italic
            If iEnd = strngLen Then Exit Do
            iEnd = iEnd + 1
        Loop
        If iEnd > iIni Then strng = strng & Mid(rng.Value2, iIni, iEnd - iIni) & "|"
        iEnd = iEnd + 1
        iIni = iEnd
    Loop

    If strng <> "" Then Italic = Split(Left(strng, Len(strng) - 1), "|")

End Function

I'm not sure what I should do within that "uppercase".

like image 966
johndoe253 Avatar asked Jun 14 '26 21:06

johndoe253


1 Answers

You would check if the characters uppercase equals the present character:

 While UCase(rng.Characters(iEnd, 1).Text) = rng.Characters(iEnd, 1).Text And Not rng.Characters(iEnd, 1).Font.Italic​

Thanks @SiddharthRoth for the assist.

Now that you have shown the full code, One more note. You should avoid naming variables, subs and function by words that are used in excel.

So change the name to Itlc or something else:

Function Itlc(rng As Range) As Variant
    Dim strng As String
    Dim iEnd As Long, iIni As Long, strngLen As Long

    strngLen = Len(rng.Value2)
    iIni = 1

    Do While iEnd <= strngLen
       Do While UCase(rng.Characters(iEnd, 1).Text) = rng.Characters(iEnd, 1).Text And Not rng.Characters(iEnd, 1).Font.Italic
            If iEnd = strngLen Then Exit Do
            iEnd = iEnd + 1
        Loop
        If iEnd > iIni Then strng = strng & Mid(rng.Value2, iIni, iEnd - iIni) & "|"
        iEnd = iEnd + 1
        iIni = iEnd
    Loop

    If strng <> "" Then Itlc = Split(Left(strng, Len(strng) - 1), "|")

End Function
like image 62
Scott Craner Avatar answered Jun 18 '26 01:06

Scott Craner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!