Option Explicit
Sub xlCellTextMgmt( _
TargetCell As Range, _
TargetWord As String, _
Optional FirstOnly As Boolean = True, _
Optional FontName As String, _
Optional FontBold As Boolean, _
Optional FontSize As Variant, _
Optional FontColor As Variant)
Dim Start As Long
Start = 0
Do
Start = InStr(Start + 1, TargetCell.Text, TargetWord)
If Start < 1 Then Exit Sub
With TargetCell.Characters(Start, Len(TargetWord)).Font
If IsNull(FontName) = False Then .Name = FontName
If IsNull(FontBold) = False Then .Bold = FontBold
If IsNull(FontSize) = False Then .Size = FontSize
If IsNull(FontColor) = False Then .ColorIndex = FontColor
End With
If FirstOnly = True Then Exit Sub
Loop
End Sub
Sub xlCellTextMgmt_Test()
Dim FirstOnly As Boolean
Dim FontColor As Variant
Dim FontName As String
Dim FontBold As Boolean
Dim FontSize As Long
Dim TargetCell As Range
Dim TargetWord As String
Set TargetCell = Selection.Cells(1)
If TargetCell.Text = "" Then
MsgBox "you must start with a cell containing some text.", vbOKOnly
Exit Sub
End If
TargetWord = Application.InputBox(Prompt:="target word(s)" & vbCrLf & vbCrLf & _
"[target word(s) can be anything from a single character" & vbCrLf & _
" to multiple words to the entire text of the target cell]", Type:=2)
FirstOnly = Application.InputBox(Prompt:="reformat ONLY the first instance?" & _
vbCrLf & vbCrLf & _
"[if True, only the first instance of " & TargetWord & _
" will be reformatted]" & vbCrLf & _
"[if False, all instances of " & TargetWord & _
" will be reformatted]", Type:=4)
FontName = Application.InputBox(Prompt:="font name #", Type:=2)
FontBold = Application.InputBox(Prompt:="bold? (True or False]", Type:=4)
FontSize = Application.InputBox(Prompt:="font size", Type:=1)
FontColor = Application.InputBox(Prompt:="Target color #", Type:=1)
Call xlCellTextMgmt(TargetCell, TargetWord, FirstOnly, FontName, FontBold, _
FontSize, FontColor)
Set TargetCell = Nothing
End Sub
|