我希望字体颜色能够反映单词的颜色。例如,每次在字符串中出现“红色”一词时,我希望红色一词的字体为红色(或以红色突出显示)。我的单元格中包含文本字符串,其中包含站点名称,截止日期和RAG状态。它们在一个单元格内,由换行符(char(10))分隔。我有基于截止日期的单元格列,以及按工作类型划分的行,因此我无法轻松地将每个文本段拆分为自己的单元格并使用条件格式,而不会破坏此表格格式。该字符串由连接文本的代码构建,然后在公式中引用。我可以编写基本的VBA,但不知道如何执行此操作,但已附加了concat代码(来自Chandoo)以说明如何构建文本字符串。
Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
For Each cell In useThis
If CStr(cell.Value) <> "" And CStr(cell.Value) <> " " Then
retVal = retVal & CStr(cell.Value) & dlm
End If
Next
If dlm <> "" Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function
有人可以建议我该如何处理吗?或建议使用其他替代方法。
首先,您需要在字符串中找到搜索词的开始位置,因此
startRed = InStr(0,searchstring,"Red",CompareMethod.Text)
然后在指定的单元格中,使用characters属性和已知长度更改颜色
With Cell.Characters(Start:= startRed, Length:= Len("Red")).Font
.Color = RGB(255,0,0)
对每种所需的颜色执行此操作,您的单元将根据需要进行更改
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句