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

我试图检查我在Excel中select的文本是否包含上标和符号,例如®™º 。 我设法find检查上标的方法如下。

 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 

任何想法如何我可以检查字符等于符号如上面突出显示。 任何build议或引用链接高度赞赏。

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 

在这里输入图像说明在这里输入图像说明

如果你正在寻找那些特定的字符,你只需循环遍历每个字符,并比较你正在search的符号列表。 例如:


 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,因为它们已经由于字符本身的性质而位于“上方”。

你可以得到chr值,像Asc(character)

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

简单地说,遍历string的所有字符并获取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