You are absolutely right! The previous macro rebuilds the cell content as plain text, which strips away any rich text formatting like specific font colors.
To preserve the font color (and other formatting like bold, italics, etc.) of the non-strikethrough text, we need to work directly with the Characters object and delete the strikethrough portions, rather than rebuilding the string.
Here's the updated VBA code:
Sub DeleteStrikethroughPreservingFormatting()
Dim ws As Worksheet
Dim cell As Range
Dim i As Long ' Use Long for character index, can be large
' Optimize macro speed
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set ws = ActiveSheet ' Or specify a sheet by name: Set ws = ThisWorkbook.Sheets("Sheet1")
On Error Resume Next ' In case a cell doesn't support rich text or is empty
For Each cell In ws.UsedRange
' Check if the cell contains text and is not a formula
If Not cell.HasFormula And Len(Trim(CStr(cell.Value))) > 0 Then
' We need to loop backwards when deleting characters,
' otherwise, the character indices will shift.
For i = Len(cell.Characters) To 1 Step -1
If cell.Characters(i, 1).Font.Strikethrough Then
cell.Characters(i, 1).Delete
End If
Next i
End If
Next cell
On Error GoTo 0 ' Reset error handling
' Restore application settings
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
MsgBox "Strikethrough text has been removed while preserving other formatting."
End Sub
Key changes and why they work:
Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual: These are standard VBA optimizations. They prevent Excel from redrawing the screen or recalculating formulas after every minor change, making the macro run much faster, especially on large worksheets.
On Error Resume Next: This is added because operations on the Characters object can sometimes fail (e.g., if a cell somehow doesn't support rich text formatting, though this is rare for cells where you've applied strikethrough). This line tells VBA to just skip to the next instruction if an error occurs within the loop for a particular cell.
If Not cell.HasFormula And Len(Trim(CStr(cell.Value))) > 0 Then:
Not cell.HasFormula: We only want to process cells with literal text, not formulas. Trying to modify characters of a formula result will error.
Len(Trim(CStr(cell.Value))) > 0: Ensures the cell actually contains some text (after converting to string and trimming whitespace).
For i = Len(cell.Characters) To 1 Step -1:
- This is the crucial part. We loop backwards from the last character to the first.
- When you delete a character, the indices of all subsequent characters shift. If you loop forwards and delete character
n, the original character n+1 becomes the new character n, and your loop would skip it. Looping backwards avoids this problem.
cell.Characters(i, 1).Delete:
- This line directly deletes the character at position
i if its Font.Strikethrough property is True.
- Because we are directly manipulating the
Characters collection within the cell, Excel preserves the existing formatting of the characters that are not deleted.
On Error GoTo 0: Resets VBA's error handling to its default behavior after the loop.
- Restore
Application settings: Crucial to set Calculation and ScreenUpdating back to their original states.
How to use it:
- Open your Excel workbook.
- Press
ALT + F11 to open the VBA editor.
- In the VBA editor, go to
Insert > Module.
- Paste the code above into the new module.
- Close the VBA editor.
- You can run the macro by pressing
ALT + F8, selecting DeleteStrikethroughPreservingFormatting from the list, and clicking Run.
This version should correctly remove only the strikethrough text while leaving the color and other formatting of the remaining text intact.