找到具有上标和下标文本的单元格

时间:2017-06-02 03:13:45

标签: excel excel-vba vba

我正在努力学习VBA。

我想在单元格文本中标记具有上标/下标的单元格。这可以使用Excel VBA吗?

1 个答案:

答案 0 :(得分:2)

如果您的细胞是

  • 所有上标,或
  • 以上标字符开头(这是古怪的 - xlPart应该找到格式化字符串的任何部分)

然后这段代码(设置为查看B列)比根据msft链接测试非常单元格中的每个字符要快得多

您可以使用Application.FindFormat.Font.Superscript = True作为上标

如果做不到这一点,我会考虑更复杂的解决方案来解析这些需要导出文本的字符。

Sub Test()

    Dim ws As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim FirstAddress As String

    Set ws = ActiveSheet
    Set rng1 = ws.Range("B:B")


    Application.FindFormat.Font.Subscript = True


    With rng1
        Set rng2 = .Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlNext, , , True)

        If rng2 Is Nothing Then
            MsgBox "None found"
        Else
            FirstAddress = rng2.Address
            Set rng3 = rng2
            Do
                Set rng2 = .Cells.Find("*", rng2, xlFormulas, xlPart, xlByRows, xlNext, , , True)
                If rng2.Address = FirstAddress Then
                    Exit Do
                Else
                    Set rng3 = Union(rng3, rng2)
                End If
            Loop
            If Not rng3 Is Nothing Then MsgBox rng3.Address
        End If
    End With
End Sub