如何修改下面的代码以在开头而不是结尾插入格式化符号?
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
有两个参数:start
和 length
在您要在开头插入的第一行,不更改现有文本/格式。所以参数应该是0
(即开头)和0
(即没有长度,所以我们不会覆盖任何东西)。
在第二行,您还想从头开始(所以再次0
),然后格式化所有已插入的字符(我们可以使用文本的len
gth 来获得它)。
最终结果:
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
输出:关于excel vba 在文本前插入格式化符号,保持原有文本的原始格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69078613/