ron
03-01-2005, 02:37 PM
Hello,
The code below bolds the string entered in an input box. I require some help to make a change.
Currently the code will bold the entered characters whether it is a whole word or part of a word. I would like to bold the whole word only. For example, if I enter the word ?in? I would only like all of the instances in the sheet of the word ?in? bolded not the letters ?in? bolded within a word.
Regards,
Ron
Sub MakeWordBold()
Dim strSearch As String
Dim searchRng As Range
Dim i As Long
Dim cel As Range
Dim LastRow As Integer
Dim LastCol As Integer
Dim Found As Boolean
Application.ScreenUpdating = False
LastRow = ActiveSheet.UsedRange.Rows.Count
LastCol = (ActiveSheet.UsedRange.Columns.Count) - 1
Set searchRng = Range(Cells(1, 1), Cells(LastRow, LastCol))
strSearch = InputBox("Please enter the text to make bold:", "Bold Text")
If strSearch = "" Then
Exit Sub
End If
For Each cel In searchRng
With cel
.Font.Bold = False
For i = 1 To Len(.Text) - Len(strSearch) Step 1
If Mid(.Text, i, Len(strSearch)) = strSearch Then
.Characters(i, Len(strSearch)).Font.Bold = True
Found = True
End If
Next i
End With
Next cel
If Found = False Then
MsgBox "No match found.", vbOKOnly
Else
MsgBox "Complete.", vbOKOnly
End If
Application.ScreenUpdating = True
End Sub
The code below bolds the string entered in an input box. I require some help to make a change.
Currently the code will bold the entered characters whether it is a whole word or part of a word. I would like to bold the whole word only. For example, if I enter the word ?in? I would only like all of the instances in the sheet of the word ?in? bolded not the letters ?in? bolded within a word.
Regards,
Ron
Sub MakeWordBold()
Dim strSearch As String
Dim searchRng As Range
Dim i As Long
Dim cel As Range
Dim LastRow As Integer
Dim LastCol As Integer
Dim Found As Boolean
Application.ScreenUpdating = False
LastRow = ActiveSheet.UsedRange.Rows.Count
LastCol = (ActiveSheet.UsedRange.Columns.Count) - 1
Set searchRng = Range(Cells(1, 1), Cells(LastRow, LastCol))
strSearch = InputBox("Please enter the text to make bold:", "Bold Text")
If strSearch = "" Then
Exit Sub
End If
For Each cel In searchRng
With cel
.Font.Bold = False
For i = 1 To Len(.Text) - Len(strSearch) Step 1
If Mid(.Text, i, Len(strSearch)) = strSearch Then
.Characters(i, Len(strSearch)).Font.Bold = True
Found = True
End If
Next i
End With
Next cel
If Found = False Then
MsgBox "No match found.", vbOKOnly
Else
MsgBox "Complete.", vbOKOnly
End If
Application.ScreenUpdating = True
End Sub