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

标签 excel vba character

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

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在您要在开头插入的第一行,不更改现有文本/格式。所以参数应该是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
输出:
enter image description here

关于excel vba 在文本前插入格式化符号,保持原有文本的原始格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69078613/

相关文章:

excel - 从 SSRS 导出到 Excel(不带标题)

vba - Outlook VbaProject.OTM 时间戳在更改时不会更新

vba - Visio 使用 VBA 以编程方式设置页面方向

ios - 在 Swift 中设置 UITextField 的最大字符长度

string - 反转用户在汇编语言中给出的字符串

excel - 在 VBA 中使用 application.match 时出现错误 2042

VBA将一组单元格作为范围传递给函数

java - 在客户端以编程方式打开 Excel 文件时,response.flushBuffer() 给我一个错误

excel - 确保公共(public) VBA 方法不会出现在 Excel 宏列表中

javascript - 输入表单中不允许有空白字符/空格