Delete red text inside cell using VBA

PC_Meister

Board Regular
Joined
Aug 28, 2013
Messages
72
Hello,

I am wondering if it is possible to delete red text inside a cell using VBA. Please note that the cell can contain red text and text with other color. So for example if the cell has "John, Joe". Only "John," is deleted from the cell. Thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Here's a small UDF i wrote which can be improved
Code:
Function RemoveRedText(rng As Range)
    Dim I As Integer
    If rng.Cells.Count = 1 Then
        For I = 1 To Len(rng)
            If rng.Characters(I, 1).Font.Color <> vbRed Then
               RemoveRedText = RemoveRedText & Mid(rng, I, 1)
            End If
        Next I
    End If
End Function
and this is what it looks like on the worksheet

Excel 2010
AB
1John DoeDoe
2Jane diatmendiatmen
3James CallagherJames
orkut
Cell Formulas
RangeFormula
B1=RemoveRedText(A1)


I noticed that the red font isn't shown(surprisingly) in the sheet displayed. In the first, "John" is red, second, "Jane" is red and in the Third "Callagher" is Red
 
Last edited:
Upvote 0
Here's a small UDF i wrote which can be improved
Code:
Function RemoveRedText(rng As Range)
    Dim I As Integer
    If rng.Cells.Count = 1 Then
        For I = 1 To Len(rng)
            If rng.Characters(I, 1).Font.Color <> vbRed Then
               RemoveRedText = RemoveRedText & Mid(rng, I, 1)
            End If
        Next I
    End If
End Function
and this is what it looks like on the worksheet
Excel 2010
AB
1John Doe Doe
2Jane diatmen diatmen
3James CallagherJames

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
orkut

Worksheet Formulas
CellFormula
B1=RemoveRedText(A1)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>



I noticed that the red font isn't shown(surprisingly) in the sheet displayed. In the first, "John" is red, second, "Jane" is red and in the Third "Callagher" is Red

Works great, I have learned something new about range.characters :)
 
Upvote 0
Works great
Does it really? You said in you first message "I am wondering if it is possible to delete red text inside a cell using <acronym title="visual basic for applications">VBA</acronym>" which sounded like you wanted to modify the text directly within the cell. Also, you said "Please note that the cell can contain red text and text with other color" and Momentman's function will not preserve other colors. If you really do want to modify the text directly within its own cell, then consider the following macro. Select the cell or cells you want to process and then run this macro...
Code:
Sub RemoveTheRedText()
  Dim X As Long, Cell As Range
  For Each Cell In Selection
    For X = Len(Cell.Text) To 1 Step -1
      If Cell.Characters(X, 1).Font.Color = vbRed Then Cell.Characters(X, 1).Text = ""
      If X > 1 Then
        If Cell.Characters(X - 1, 2).Text = "  " Then
          Cell.Characters(X, 1).Text = ""
        End If
      ElseIf Cell.Characters(X, 1).Text = " " Then
        Cell.Characters(X, 1).Text = ""
      End If
    Next
  Next
End Sub
Note: I added code to make sure leading spaces are removed and double spaces collapsed down to single spaces.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,563
Members
449,318
Latest member
Son Raphon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top