gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:45:48 26 4
gpt4 key购买 nike

我的单元格将包含以下值

"Image not allowed|png"

我想单独更改 |png 的颜色或 "|" 之后的任何内容

现在我尝试使用下面的代码更改字体颜色

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

它将更改整个单元格的字体颜色,是否可以使用 VBA 仅更改选定的文本颜色(|png)?

最佳答案

您可以使用Characters单元格的属性,例如:

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

这应该是一个好的开始:

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

关于excel - 更改单元格中部分文本的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30567552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com