从单元格中获取一个值,如果单元格中有“显示”,则将其替换为“更改”。
单元格有不止一种颜色,我必须保持所有单词的颜色(除了显示)。
text = Cells(row, col).value
' text have other colors then black – and I want to keep that
text = “hi, this test show how the text can look like”
' want to replace “show” to “change” and keep the text coloring.
' similarly I can set the color with
Cells(row, col).Characters(15, Len(show)).Font.Color = vbBlue
' Can I change the text in a similar way?
' If I use:
Cells(row, col).value = text
' All color is gone!
最佳答案
这是一个示例代码,它将更改文本而不更改文本其他部分的当前颜色或其他字体属性。我将带有字体颜色的示例文本字符串放入单元格 A1 中。
Public Sub test()
Dim str As String, pos As Long
str = Range("A1").Value
pos = InStr(str, "show")
Range("A1").Characters(pos, 4).Insert ("change")
End Sub
注意 Characters().Insert() 行的重要方面。 Characters(start, length) 是您要删除的部分 AND SIZE,Insert 将新的(和更长的)文本放在它的位置。
关于excel - 仅更改单元格中字符串的特定部分,保持原样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29274038/