How to extract text of some color from single cell?

roberto.delagarza

New Member
Joined
May 21, 2012
Messages
2
I need to extract some text from singe cells wich has been marked with another color, let's say red. Do you have a simple vba macro subroutine which can extract the text and write it in another cell? I will apply this sobroutine in the entire database field. Sometimes the text is contiguos in the cell, sometimes it isn't. Thanks !!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi, welcome to the board!

Code:
Function GetColorText(r As Range) As String
Dim s As String, t As String, x As Long
t = r.Text
For x = 1 To Len(t)
    If r.Characters(x, 1).Font.Color = vbRed Then s = s & Mid(t, x, 1)
Next
GetColorText = s
End Function
 
Upvote 0
Thank you very much. It will be a very time-consuming task to separate the different text according to its color. I could modificate your Function and separate red color and black color and it was very easy. Again thanks. I imaginate it will be a way in vba to make it but I recognize I was far from solve the problem of build the macro.
 
Upvote 0
If you need it to return everything that is not black, no matter what the other color is, try this:

Code:
Function GetColorText(r As Range) As String
Dim s As String, t As String, x As Long
t = r.Text
For x = 1 To Len(t)
    If r.Characters(x, 1).Font.Color <> vbBlack Then s = s & Mid(t, x, 1)
Next
GetColorText = s
End Function
 
Upvote 0
If you need it to return everything that is not black, no matter what the other color is, try this:

Code:
Function GetColorText(r As Range) As String
Dim s As String, t As String, x As Long
t = r.Text
For x = 1 To Len(t)
    If r.Characters(x, 1).Font.Color <> vbBlack Then s = s & Mid(t, x, 1)
Next
GetColorText = s
End Function

The code extracts colored text but without space between two words in case the words are away from each other.

Please help with a solution which would keep the words separate.
 
Last edited:
Upvote 0
The code extracts colored text but without space between two words in case the words are away from each other.

Please help with a solution which would keep the words separate.
If all the letters within any single word are always the same color (that is, no single word has two or more differently colored letters), then this UDF (user defined function) should be faster than the one Scott posted in Message #4 and it puts a space between each returned word.
Code:
[table="width: 500"]
[tr]
	[td]Function GetColorText(R As Range) As String
  Dim X As Long, Pos As Long, Words() As String
  Words = Split(R.Text)
  Pos = 1
  For X = 0 To UBound(Words)
    If R.Characters(Pos, 1).Font.Color <> vbBlack Then GetColorText = GetColorText & " " & Words(X)
    Pos = Pos + Len(Words(X)) + 1
  Next
  GetColorText = Trim(GetColorText)
End Function[/td]
[/tr]
[/table]
 
Upvote 0
If all the letters within any single word are always the same color (that is, no single word has two or more differently colored letters), then this UDF (user defined function) should be faster than the one Scott posted in Message #4 and it puts a space between each returned word.
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function GetColorText(R As Range) As String
  Dim X As Long, Pos As Long, Words() As String
  Words = Split(R.Text)
  Pos = 1
  For X = 0 To UBound(Words)
    If R.Characters(Pos, 1).Font.Color <> vbBlack Then GetColorText = GetColorText & " " & Words(X)
    Pos = Pos + Len(Words(X)) + 1
  Next
  GetColorText = Trim(GetColorText)
End Function
[/TD]
[/TR]
</tbody>[/TABLE]

Amazing stuff! Really worked much faster that earlier codes.. Thanks Rick.
 
Upvote 0
If all the letters within any single word are always the same color (that is, no single word has two or more differently colored letters), then this UDF (user defined function) should be faster than the one Scott posted in Message #4 and it puts a space between each returned word.
Code:
[table="width: 500"]
[tr]
    [td]Function GetColorText(R As Range) As String
  Dim X As Long, Pos As Long, Words() As String
  Words = Split(R.Text)
  Pos = 1
  For X = 0 To UBound(Words)
    If R.Characters(Pos, 1).Font.Color <> vbBlack Then GetColorText = GetColorText & " " & Words(X)
    Pos = Pos + Len(Words(X)) + 1
  Next
  GetColorText = Trim(GetColorText)
End Function[/td]
[/tr]
[/table]
Rick this is exactly what I have been looking for !!!!! Thank you so much. But I am now having problems when trying to add ElseIf into the VBA. I have multiple colors I need to look for and extract not just one. Help !!!!!!
 
Upvote 0
Tell us all of the colors and what should or should not happen for each of them.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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