简体   繁体   English

更改单元格中部分文本的字体颜色

[英]Change font color for a part of text in cell

I have cells which will contain the below value我有包含以下值的单元格

"Image not allowed|png"

I want to change the color of |png alone or whatever comes after "|"我想单独更改|png的颜色或“|”之后的任何内容

Now i am trying to change the font color using the below code现在我正在尝试使用以下代码更改字体颜色

Cells(4,2).Font.Color = RGB(255, 50, 25)

It will change the entire cells font color, Is it possible to change only the selected text color( |png ) using VBA?它将更改整个单元格的字体颜色,是否可以使用 VBA 仅更改选定的文本颜色( |png )?

You can use Characters cell's property like :您可以使用Characters单元格的属性,例如:

Cells(1,1).Characters(Start:=2, Length:=3).Font.Color = RGB(255, 0, 0)

This should be a good start :这应该是一个好的开始:

Sub vignesh()
Dim StartChar As Integer, _
    LenColor As Integer

For i = 1 To 5
    With Sheets("Sheet1").Cells(i, 1)
        StartChar = InStr(1, .Value, "|")
        If StartChar <> 0 Then
            LenColor = Len(.Value) - StartChar + 1
            .Characters(Start:=StartChar, Length:=LenColor).Font.Color = RGB(255, 0, 0)
        End If
    End With
Next i

End Sub

Yes this is possible.是的,这是可能的。 A good way to explore the Excel object model is to use the macro recorder to record a macro where you manually carry out the manipulation you're interested in.探索 Excel 对象模型的一个好方法是使用宏记录器记录一个宏,您可以在其中手动执行您感兴趣的操作。

In this case, you can use:在这种情况下,您可以使用:

Cell.Characters(Start:=1, Length:=5).Font

to set font properties of a substring in a cell.设置单元格中子字符串的字体属性。

Is it possible to change only the selected text color是否可以只更改选定的文本颜色

Simple简单的

Option Explicit
Sub Test()
    With Selection.Font
        .ColorIndex = 3 
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM