PDA

View Full Version : [SOLVED:] Search for specific words in worksheet and make bold



HTSCF Fareha
12-10-2020, 04:05 AM
I'm trying to allow searching of a spreadsheet to find a word or multiple words as required by userform inputbox, to make the entire text contents of any cell found with the matching word(s) to bold.

Here is what I have so far, although it's not working.


' Provide option to search for specific word(s) and make contents of that cell bold
Private Sub btnSearchText_Click()

Dim ws As Worksheet
Dim rng As Range
Dim iPos As Long
Dim vSearchText As Variant
Dim i As Long

vSearchText = Application.InputBox("Enter words to highlight", "Triage", "", Type:=2)

Application.ScreenUpdating = False

Set rng = Worksheets("Data").rng("$A$1").Activate
For Each rng In ws

For i = 0 To UBound(vSearchText)
iPos = InStr(1, rng, vSearchText(i), vbTextCompare)

If iPos Then
Do
rng.Characters(iPos, Len(vSearchText(i))).Font.FontStyle = "Bold"
iPos = InStr(iPos + 1, rng, vSearchText(i), vbTextCompare)
Loop Until iPos = 0

ElseIf vSearchText <> vSearchText Then

answer = MsgBox((vSearchText) + " was not found", vbExclamation + vbOKOnly, "Triage")
Exit Sub
End If

Next i

Next rng
Application.ScreenUpdating = True

Exit Sub
End Sub

HTSCF Fareha
12-10-2020, 09:21 AM
Found the answer on another website.

https://excelribbon.tips.net/T012105_Making_All_Occurrences_Bold