Excel VBA - 检查字符是否等于符号

时间:2014-11-14 08:31:37

标签: excel vba excel-vba char symbols

我试图检查我在Excel中选择的文字是否包含上标和符号,例如 ®™º 。我设法找到了检查上标的方法,如下所示。

For Each c In rngSel
lChar = Len(c.Value)
    For lCount = 1 To lChar
        With c.Characters(lCount, 1).Font

            'Superscript
            If .Superscript Then
            str_Test = str_Test & "" & lCount
            isFlag = "Y"
            .ColorIndex = 3
            total_super_count = total_super_count + 1

我知道如何检查角色是否与上面突出显示的符号相等。任何建议或参考链接都非常感谢。

EDITED

Columns("A:S").Select
Rows("1:50").Select
Set rngSel = Selection
For Each c In rngSel
lChar1 = Len(c.Value)
    'For lCount = 1 To lChar
     '   With c.Characters(lCount, 1).Font
     For lCount1 = 1 To c.Characters.Count
     With c.Characters(lCount1, 1)


            'Test the character
            If .Text Like "°" Then
count_symbol = count_symbol + 1
            'Do something
                .Font.Color = vbRed

            End If
        End With
    Next lCount1
Next c

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

如果您要查找这些特定字符,只需遍历每个字符并与您要搜索的符号列表进行比较。 e.g:


For Each C In R
    For I = 1 To C.Characters.Count
        With C.Characters(I, 1)

            'Test the character
            If .Text Like "[®™º]" Then

            'Do something
                .Font.Color = vbRed

            End If
        End With
    Next I
Next C

你应该注意,你正在寻找的上面显示的特定符号将显示font.superscript = FALSE,因为它们已经在"线上方#34;凭借角色本身的性质。

答案 1 :(得分:0)

您可以获得角色的chr值,例如Asc(character)

如果您查看http://www.gtwiki.org/mwiki/?title=VB_Chr_Values,则会看到chr值列表。

简单地说,循环遍历字符串的所有字符并获取chr值,如果它高于122,那么它就是符号!

另请注意,您可能需要检查一些其他字符(低于122),因此请完整查看列表。 EG,从0到47!

因此,在我的Excel工作表中,A1的值为5™B

这就是你想要的

Sub DetectSymbols()

Dim val As String
val = Range("A1").Value                 'Get value or walk the plank !

Dim i As Integer
For i = 1 To Len(val)                   'Loop through each character in string, ya sea dogs
    Dim c As String
    c = Mid(val, i, 1)  
    Dim chrVal As Integer
    chrVal = Asc(c)
    If (chrVal > 122) Then              'Only checking > 122 to keep it simple
        MsgBox (c & " is a symbol")     'Aye aye, captain, we found the treasure!
    End If
Next i

End Sub