PDA

View Full Version : [SOLVED:] Help Required : Highlight a part of text with in a cell (case sensitive problem)



anish.ms
11-30-2020, 12:39 PM
Hi, The below code copied from the internet highlights the input keyword but it is subject to case sensitive. For example in the attached file, I have keyword "DOA" but if I input in small letter 'doa', the text is not getting highlighted. Can somebody please help with a solution ?



Sub HighlightStrings()
Application.ScreenUpdating = False
Dim Rng As Range
Dim cFnd As String
Dim xTmp As String
Dim x As Long
Dim m As Long
Dim y As Long
cFnd = InputBox("Enter the text string to highlight")
y = Len(cFnd)
For Each Rng In Selection
With Rng
m = UBound(Split(Rng.Value, cFnd))
If m > 0 Then
xTmp = ""
For x = 0 To m - 1
xTmp = xTmp & Split(Rng.Value, cFnd)(x)
.Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
xTmp = xTmp & cFnd
Next
End If
End With
Next Rng
Application.ScreenUpdating = True
End Sub

Paul_Hossler
11-30-2020, 12:50 PM
Option Explicit
Option Compare Text ' <<<<<<<<<<<<<<<<<<<<<<<<<


Sub HighlightStrings()
Application.ScreenUpdating = False
Dim Rng As Range
Dim cFnd As String
Dim xTmp As String
Dim x As Long
Dim m As Long
Dim y As Long

cFnd = InputBox("Enter the text string to highlight")

y = Len(cFnd)

For Each Rng In Selection
With Rng
m = UBound(Split(Rng.Value, cFnd))
If m > 0 Then
xTmp = ""
For x = 0 To m - 1
xTmp = xTmp & Split(Rng.Value, cFnd)(x)
.Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
xTmp = xTmp & cFnd
Next
End If
End With
Next Rng
Application.ScreenUpdating = True
End Sub

anish.ms
12-01-2020, 11:08 AM
Thanks a ton Paul