excel vba 在文本前插入格式化符号,保持现有文本的原始格式

excel vba Insert formatted symbol in front of text keeping original formats of pre-existing text

如何修改下面的代码以在开头而不是结尾插入格式化符号?

Sub I___TickRedAFTERText_KeepsOtherCharFormatting()

ActiveCell.Characters(ActiveCell.Characters.Count + 1, 1).Insert (" P ")

'specify location and format the new character
     With ActiveCell.Characters(ActiveCell.Characters.Count - 1, 1).Font 'second to last character in cell
        .Name = "Wingdings 2"
        .Bold = True
        .Color = -16776961
    End With
End Sub

ActiveCell.Characters 有两个参数:startlength

在您要插入的第一行开头,不对现有 text/formatting 进行任何更改。所以参数应该是0(即开头)和0(即没有长度,所以我们不会覆盖任何东西)。

在第二行你也想从头开始(所以又是0)然后格式化所有插入的字符(我们可以使用第length获取此文本)。

最终结果:

Sub I___TickRedAFTERText_KeepsOtherCharFormatting()

    ActiveCell.Characters(0, 0).Insert(" P ")

    'specify location and format the new character
     With ActiveCell.Characters(0, Len(" P ")).Font
        .Name = "Wingdings 2"
        .Bold = True
        .Color = -16776961
    End With
    
End Sub

输出: