Excel / vba查找字符,替换并在上标后加两个字符

时间:2013-06-02 20:23:04

标签: excel vba excel-vba

我想要的是找到“#”char,删除它并在上标后加2个字符。 我有这个代码:

    pos = InStr(mycell.Value, "#")
    If pos > 0 Then
        mycell.Replace What:="#", Replacement:=""
        mycell.Characters(Start:=pos + 1, Length:=2).Font.Superscript = True
    End If

当我只替换 - 工作。当我只做一些chars上标 - 作品。当两者都 - 只替换。

1 个答案:

答案 0 :(得分:1)

pos + 1需要为pos,因为您已使用Replace方法删除了一个字符。经过测试,似乎在Excel 2010中运行良好。

pos = InStr(myCell.Value, "#")
If pos > 0 Then
    myCell.Replace What:="#", Replacement:=""
    myCell.Characters(Start:=pos, Length:=2).Font.Superscript = True
End If