Is there a way to change the color of the text in ALL of the cells in an Excel sheet? Something like finding a text and change the forecolor of the searched text only for the cells of the Excel sheet.
-
Can the text you are searching for be present in a different word, that shouldn't be coloured? E.g. say the word you are searching for is 'cat'. Should 'cat' in the word 'catering' also be coloured? If yes, then you can do find/replace. If no, then you will need VBA (write code/macro) I believe. – Jerry Oct 31 '17 at 07:43
-
Can you give the solution to change the color of the text with find/replace in excel sheet. – Srihari Oct 31 '17 at 07:52
-
@Ralph Built in features do not allow you to change only part of a cell, so VBA would be the only way – ashleedawg Oct 31 '17 at 08:25
-
@Jerry my test (in answer edited below) shows the entire cell's formatting being changed by find-and-replace, even by a partial match, unless I'm missing something? – ashleedawg Oct 31 '17 at 08:50
-
I don't think we have a solution with find/replace or with formulas. @Jerry can you post the VBA code and where should it be added and how should it be executed. – Srihari Oct 31 '17 at 09:01
-
@Srihari & Ralph -- I edited my answer (below) to add an alternative with VBA. It doesn't meet the conditions of the OP, but is the closest possible solution – ashleedawg Oct 31 '17 at 09:10
-
1Sorry, I was mistaken. A cheap alternative to VBA would be to copy the range into word and do the replace there, then copy/paste back into excel. And no, I'm not that conversant in VBA to give a proper answer (and sorry for the late reply, something came up earlier that I had to attend to). – Jerry Oct 31 '17 at 09:25
1 Answers
Change the color of all the cells of an excel sheet:
As an example:
- Select the entire worksheet or a range of cells.
- On the
Home
tab chooseConditional Formatting
- Click
New Rule...
- Click
Use a formula to determine which cells to format
- Under
Format cells where this value is true
enter formula: =(LEN($A$1)>0) - Click
Format
and go to theFill
tab - Choose a fill color. Click OK, OK.
Now if cell A1 has any value in it, the entire range selected in step 1 will change color. You can specify different cell ranges, criteria, or formatting, as necessary. (For example, text color instead of fill color)
Edit #1:
Re: Find & Replace to change color of part of a cell
Find & Replace can search for, or replace, cell formatting, but the replacement formatting affects the entire cell.
Result: (whole cell changed)
Edit #2a:
You said "no VBA" but for the sake of sharing possible alternative solutions, here is how this could be accomplished with VBA. This method loops through all cells in ActiveSheet.UsedRange
:
Sub SearchReplace_Color_PartialCell()
Const textToChange = "cat"
Const newColor = vbRed
Dim c As Range
'loop throgh all cells that have data
For Each c In ActiveSheet.UsedRange.Cells
If InStr(c.Value, textToChange) > 0 Then 'if text exists in cell
' then change the color of that text
c.Characters(InStr(c.Value, textToChange), Len(textToChange)).Font.Color = newColor
End If
Next c
End Sub
When run on 10,000 cells, each with varying length strings, all with the word "cat" in the middle, this method ran in 2.6797 seconds.
Edit #2b:
Another VBA solution, using .Find
and .FindNext
to loop through cells with data:
Sub FindReplace_Color_PartialCell()
Const textToChange = "cat"
Const newColor = vbRed
Dim c As Range, firstAddress As String
With ActiveSheet.Cells
Set c = .Find(textToChange, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Characters(InStr(c.Value, textToChange), Len(textToChange)).Font.Color = vbGreen
Set c = .FindNext(c)
If c Is Nothing Then
GoTo DoneFinding
End If
Loop While c.Address <> firstAddress
End If
DoneFinding:
End With
End Sub
When run on 10,000 cells each with varying length strings, all with the word "cat" in the middle, this method ran in 8.7021 seconds.
Edit #2c:
Modified to continue searching a cell until no further matches are found (instead of moving to next cell after one replacement):
Sub SearchReplace_Color_PartialCell()
'modified to catch multiple occurences of search term within the single cell
Const textToChange = "cat"
Const newColor = vbGreen
Dim c As Range 'the cell we're looking at
Dim pos As Integer 'current position#, where we're looking in the cell (0 = Not Found)
Dim matches As Integer 'count number of replacements
For Each c In ActiveSheet.UsedRange.Cells 'loop throgh all cells that have data
pos = 1
Do While InStr(pos, c.Value, textToChange) > 0 'loop until no match in cell
matches = matches + 1
pos = InStr(pos, c.Value, textToChange)
c.Characters(InStr(pos, c.Value, textToChange), Len(textToChange)).Font.Color = _
newColor ' change the color of the text in that position
pos = pos + 1 'check again, starting 1 letter to the right
Loop
Next c
MsgBox "Replaced " & matches & " occurences of """ & textToChange & """"
End Sub

- 20,365
- 9
- 72
- 105
-
This is an answer. But this is an answer to another question (not an answer to the question asked above). It is like someone asking for directions to the airport and you tell him that you don't know and offer directions to the train station instead. Well, let's see if the OP's requirements are met with this alternative solution. – Ralph Oct 31 '17 at 08:43
-
sorta, yeah, although demonstrating the closest possible solution that exists. About to edit the answers with an example of find-and-replace not changing color of "part cells" – ashleedawg Oct 31 '17 at 08:45
-
1I have add the VBA method. It is working what i expected. Thanks @ashleedawg – Srihari Oct 31 '17 at 10:15
-
Good to hear! Thanks, I learned some stuff too! (and thanks @Ralph; I wasn't trying to "prove" anything as much as I like a challenge!) – ashleedawg Oct 31 '17 at 10:17
-
@ashleedawg The two time comparisons are not equal matches. In #2a you are only going through `ActiveSheet.UsedRange.Cells` while you are using the `Find` method in #2b on `ActiveSheet.Cells`. Hence, `Find` seems to be slower. If you update #2b to search only the `UsedRange` then you'll see that `Find` is much faster (naturally). And this is the way it should be as you don't have to go through all `cells` one-by-one as in #2a. – Ralph Oct 31 '17 at 12:11
-
@ashleedawg It is not working with text being multiple times in the same cell. like when we have 'cat' word at two places in the same cell it is not replacing. It is replacing the first one and leaving the second one and going to another cell. – Srihari Oct 31 '17 at 12:21
-
@Ralph - when you "requested" that I use `Find`, it was implied to be as an *alternative* to `UsedRange` as opposed to using the complimentary, so that's what I compared. With enough time & desire, I'm sure there are methods much faster than both, but it becomes pointless. We're not managing data for a huge corporation, we are not professors. – ashleedawg Nov 01 '17 at 05:29
-
@Ralph: I enjoy helping with (or providing) solutions in this field since I find it easy and I like a challenge. I'm confident that the majority of OPs here are not only satisfied with non-textbook solutions that _accomplish the job_, but many _even learn things_ by other people writing code for them (like I did - 100% Google). I only started indenting code recently solely since I was tired of the *skilled* people complaining (not those asking the questions). I understand & appreciate the reasoning for strict rules here but, I am motivated to "pay it forward" to those who appreciate the help. – ashleedawg Nov 01 '17 at 05:34
-
@Srihari - I'm glad you were able to find an [almost complete] solution! I already forgot about this code but I can take another look, I'd imagine it's an easy chance to match multiples in a cell. Which VBA method are you using? #2a or #2b ? – ashleedawg Nov 01 '17 at 05:38
-
@Srihari - Hopefully we don't _get in trouble_ for this, I'm not sure but I would imagine that *refining* an answer is perfectly allowed (and you don't come across an unappreciative or demanding!) Otherwise, I don't know options since there's nowhere to discuss this privately that I know of since we can't exchange emails or such: Already today someone's question was put "on hold" (removed from search or answers etc) because *I* posted a clarifying picture as an Answer instead of a Comment. Silly... some people are here to help anmd/or learn, Others would rather invest their time in policing. – ashleedawg Nov 01 '17 at 05:45
-
@Srihari -- check out example #2c. Note the additional loop: [pos] = the position that the word in found in the cell, using `InStr`, which returns Zero if there is no match. So, when there's a match, we move one character over, search again, and repeat until no match (when pos=0). Let me know if you have any questions. – ashleedawg Nov 01 '17 at 06:24
-
@Ralph -- Final thought on that, IMO Srihari is polite, eager, and is learning from these examples, thus I am glad to help. If you feel obligated to school a newbie on how to act here, there are better uses of your time. IMO [the OP on this question](https://stackoverflow.com/q/47028831/8112776) could use some feedback for wasting people's time for no valid reason whatsoever. Also don't take offense to my comments, we are all entitled to be here for whatever reason we want. And yes, I will delete these comments later, once again I see no alternative method of 1-1 communication here. – ashleedawg Nov 01 '17 at 06:35
-
@ashleedawg nice solution, is it possible to add an input window to set the search word and color? So this function is still not available in Excel 2020? – Herman Toothrot Jun 03 '20 at 10:04
-