View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Replace Font within Cell

You could use a macro that finds the cell with the word and then italices the
characters in that word in that cell.

If you want to try:

Option Explicit
Sub testme()

Dim myRng As Range
Dim FoundCell As Range
Dim FirstAddress As String
Dim myWords As Variant
Dim wCtr As Long
Dim wks As Worksheet
Dim StartPos As Long

Set wks = Worksheets("sheet1")

'change this to the list of words to find
myWords = Array("qwer", "asdf")

With wks
'change this to the range that should be inspected
Set myRng = .Range("L:L")

With myRng
For wCtr = LBound(myWords) To UBound(myWords)
FirstAddress = ""

With .Cells
Set FoundCell = .Find(What:=myWords(wCtr), _
after:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If FoundCell Is Nothing Then
'do nothing, it wasn't found
MsgBox myWords(wCtr) & " wasn't found!"
Else
FirstAddress = FoundCell.Address
Do
StartPos = InStr(1, FoundCell.Value, _
myWords(wCtr), vbTextCompare)
If StartPos = 0 Then
'this shouldn't happen,
'since the .find worked ok
Else
With FoundCell.Characters _
(Start:=StartPos, _
Length:=Len(myWords(wCtr))).Font
.FontStyle = "Italic"
End With

'look for the next one
Set FoundCell = .FindNext(after:=FoundCell)

If FirstAddress = FoundCell.Address Then
'at the first address
Exit Do
End If
End If
Loop
End If
End With
Next wCtr
End With
End With

End Sub

If you're new to macros:

Debra Dalgleish has some notes how to implement macros he
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

(General, Regular and Standard modules all describe the same thing.)

Stephanie M wrote:

Is it possible to replace only specific characters/text within a cell? For
example I have several cells that need to have one word in italtics, but when
I use find and replace, all words in the cell are changed to italics.


--

Dave Peterson