vba 是否可以在 Excel 单元格中执行多个彩色文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9469682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to do multiple colored text within an Excel cell?
提问by phan
I have string strInfo, which contains "Employee John Maybach".
我有字符串 strInfo,其中包含“Employee John Maybach”。
How do I make the "Employee" part black text, and the "John Maybach" part red?
如何使“员工”部分为黑色文本,“约翰迈巴赫”部分为红色?
The "Employee" part will always remain constant, but the employee's name part will change such that it may be a 2-part name (John Doe), or a 3-part name (John Allen Doe), or just a first name (John).
“员工”部分将始终保持不变,但员工的姓名部分会发生变化,因此它可能是由 2 部分组成的姓名 (John Doe),或由 3 部分组成的姓名 (John Allen Doe),或者只是一个名字 (约翰)。
I want the word "Employee" to be always black, but the rest of the text in the cell, the name part, to be red. Is this possible?
我希望“员工”一词始终为黑色,但单元格中的其余文本(姓名部分)为红色。这可能吗?
回答by assylias
The macro recorder is your friend:
宏记录器是您的朋友:
Dim fixedLength As Long
fixedLength = Len("Employee")
ActiveCell.FormulaR1C1 = "Employee Some Employee"
With ActiveCell.Characters(Start:=fixedLength + 2, Length:=Len(ActiveCell) - fixedLength - 1).Font
.Color = vbRed
End With