是否可以在Excel单元格中执行多个彩色文本?

时间:2022-11-13 22:21:08

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).

“员工”部分将始终保持不变,但员工的姓名部分将更改为可能是由两部分组成的名称(John Doe),或者由三部分组成的名称(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?

我希望“员工”这个词总是黑色的,但是单元格中的其余部分,即名称部分,都是红色的。这可能吗?

1 个解决方案

#1


7  

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

#1


7  

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