How to find and replace text then preserve formatting in Excel?
Find and replace preserve formatting with Macro code
Navigation pane -- Find and Replace
|Kutools for Excel's advanced Find and Replace function, can help you find and
replace a value across multiple sheets and workbooks.
Recommended Excel Productivity Tools
There is no other methods but macro code can help you to find and replace text and preserve formatting.
1. Press Alt + F11 keys to open the Microsoft Visual Basic for Application window.
2. Click Insert > Module, and paste below code to the script.
VBA: Find and replace preserve formatting
Sub CharactersReplace(Rng As Range, FindText As String, ReplaceText As String, Optional MatchCase As Boolean = False) 'UpdatebyExtendoffice20160711 Dim I As Long Dim xLenFind As Long Dim xLenRep As Long Dim K As Long Dim xValue As String Dim M As Long Dim xCell As Range xLenFind = Len(FindText) xLenRep = Len(ReplaceText) If Not MatchCase Then M = 1 For Each xCell In Rng If VarType(xCell) = vbString Then xValue = xCell.Value K = 0 For I = 1 To Len(xValue) If StrComp(Mid$(xValue, I, xLenFind), FindText, M) = 0 Then xCell.Characters(I + K, xLenFind).Insert ReplaceText K = K + xLenRep - xLenFind End If Next End If Next End Sub Sub Test_CharactersReplace() Dim xRg As Range Dim xTxt As String Dim xCell As Range On Error Resume Next If ActiveWindow.RangeSelection.Count > 1 Then xTxt = ActiveWindow.RangeSelection.AddressLocal Else xTxt = ActiveSheet.UsedRange.AddressLocal End If Set xRg = Application.InputBox("Select a range:", "Kutools for Excel", xTxt, , , , , 8) If xRg Is Nothing Then Exit Sub Call CharactersReplace(xRg, "KK", "Kutools", True) End Sub
3. Press F5 key, a dialog pops out for you to select a range to find and replace, see screenshot:
4. Click OK, and the specific string in the selected cell are replace with other and keep the formatting.
Tip: In the code, KK is the string you want to find, and Kutools is the string you want to replace with, you can change them as you need.