[英]Change Font Of Multiple Sub Strings in Excel Cell
我有一種字體可以顯示特定的符號。 它們以標准/默認字體鍵入到工作表中。 我能夠識別這些字符,然后將這些字符的字體更改為我的符號字體,但是當單元格中有多個已識別的符號時,使其無法正常工作會遇到一些麻煩。
例如,單元格可能包含:
此值為X和Y,以及Z
我只需要更改X,Y和Z的字體。
這是我目前通過vba更改字符字體的方式:
Sub InsertFont(insertRange As Range, symbolText As String, symbolPosition As Integer)
Dim cellText As String
Dim newValue As String
cellText = insertRange.Value2
newValue = Replace(cellText, symbolText, SymbolDict.Item(symbolText), 1, 1)
insertRange.Value2 = newValue
With insertRange.Characters(symbolPosition, Len(SymbolDict.Item(symbolText))).Font
.Name = "MyFont"
End With
End Sub
問題是每次更改字體后,單元格的其余部分將恢復為默認字體! 我如何才能使字體更改適用於所有更改?
最終結果:
此值為X和Y ,,
@TimWilliams通過評論的建議,這是可行的解決方案:
Sub InsertSymbol(insertRange As Range, symbolText As String, symbolPosition As Integer)
Dim cellText As String
Dim newValue As String
insertRange.Characters(symbolPosition, Len(symbolText)).Delete
insertRange.Characters(symbolPosition, 0).Insert SymbolDict.Item(symbolText) '2nd parameter 0 to not overwrite any characters
insertRange.Characters(symbolPosition, 1).Font.Name = "My Symbol Font Name"
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.