仅更改单元格中字符串的特定部分,保持原样
Change only a specific part of a string in a cell, keep rest as it is
从单元格中取值,如果单元格中有 "show",则将其替换为 "change"。
单元格有不止一种颜色,我必须保留所有单词的颜色(显示除外)。
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 将新的(和更长的)文本放在它的位置。
从单元格中取值,如果单元格中有 "show",则将其替换为 "change"。 单元格有不止一种颜色,我必须保留所有单词的颜色(显示除外)。
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 将新的(和更长的)文本放在它的位置。