Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Ole
 
Posts: n/a
Default Coloring words within the text of cells


Hi!

I have a spreadsheet with cells that contain text, and I want to color
some words within the cells. For example, I want to color the word
"sample", whereever it occurs within the text of a cell. Now, if there
were only one word per cell, I could use conditional formatting, but as
it is, I'm stumped. Does anyone know how to do this?

Thanks,

Ole


--
Ole
------------------------------------------------------------------------
Ole's Profile: http://www.excelforum.com/member.php...o&userid=32292
View this thread: http://www.excelforum.com/showthread...hreadid=520464

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Coloring words within the text of cells

Saved from a previous post:

If you have a list of words you know you want to highlight, you could use a
macro to cycle through all the words:

Option Explicit
Option Compare Text
Sub testme()

Application.ScreenUpdating = False

Dim myWords As Variant
Dim myRng As Range
Dim foundCell As Range
Dim iCtr As Long 'word counter
Dim cCtr As Long 'character counter
Dim FirstAddress As String
Dim AllFoundCells As Range
Dim myCell As Range

'add other words here
myWords = Array("widgets","assemblies","another","word","her e")

Set myRng = Selection

On Error Resume Next
Set myRng = Intersect(myRng, _
myRng.Cells.SpecialCells(xlCellTypeConstants, xlTextValues))
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "Please choose a range that contains text constants!"
Exit Sub
End If

For iCtr = LBound(myWords) To UBound(myWords)
FirstAddress = ""
Set foundCell = Nothing
With myRng
Set foundCell = .Find(what:=myWords(iCtr), _
LookIn:=xlValues, lookat:=xlPart, _
after:=.Cells(.Cells.Count))

If foundCell Is Nothing Then
MsgBox myWords(iCtr) & " wasn't found!"
Else
Set AllFoundCells = foundCell
FirstAddress = foundCell.Address
Do
If AllFoundCells Is Nothing Then
Set AllFoundCells = foundCell
Else
Set AllFoundCells = Union(foundCell, AllFoundCells)
End If
Set foundCell = .FindNext(foundCell)

Loop While Not foundCell Is Nothing _
And foundCell.Address < FirstAddress
End If

End With

If AllFoundCells Is Nothing Then
'do nothing
Else
For Each myCell In AllFoundCells.Cells
For cCtr = 1 To Len(myCell.Value)
If Mid(myCell.Value, cCtr, Len(myWords(iCtr))) _
= myWords(iCtr) Then
myCell.Characters(Start:=cCtr, _
Length:=Len(myWords(iCtr))) _
.Font.colorindex = 3
End If
Next cCtr
Next myCell
End If
Next iCtr
Application.ScreenUpdating = True

End Sub

This line:
myCell.Characters(Start:=cCtr, _
Length:=Len(myWords(iCtr))) _
.Font.colorindex = 3
changes the color.



If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ole wrote:

Hi!

I have a spreadsheet with cells that contain text, and I want to color
some words within the cells. For example, I want to color the word
"sample", whereever it occurs within the text of a cell. Now, if there
were only one word per cell, I could use conditional formatting, but as
it is, I'm stumped. Does anyone know how to do this?

Thanks,

Ole

--
Ole
------------------------------------------------------------------------
Ole's Profile: http://www.excelforum.com/member.php...o&userid=32292
View this thread: http://www.excelforum.com/showthread...hreadid=520464


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
vlookup on large text in cells Gus Excel Worksheet Functions 2 February 23rd 06 06:55 PM
merged cells into one text cell, size varies dependant on text dat Jazzylady825 Excel Discussion (Misc queries) 0 December 9th 05 08:26 PM
checking that cells have a value before the workbook will close kcdonaldson Excel Worksheet Functions 8 December 5th 05 04:57 PM
question about removing text from cells, leaving numbers JPN5804 Excel Discussion (Misc queries) 3 November 28th 05 05:55 PM
Linked cells and text boxes Alexlondon11 Excel Discussion (Misc queries) 2 November 23rd 05 04:10 PM


All times are GMT +1. The time now is 10:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"