I have written a macro that highlights keywords in column "Y". Script does work and does exactly what I need, but it slows down excel a lot, like it is still doing something. My guess is it has to do with FOR loop, but I'm not sure how to fix it.
My VBA knowledge is very limited and this is as far as I got with googling for solution. I am hopeful someone can help me with my code.
Sub HighlightKeywords()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Dim sPos As Long, sLen As Long
Dim rng As Range
Dim findMe As String
Dim i As Integer
Dim t As Integer
Dim SearchArray
SearchArray = Array("WORD1", "WORD2")
For t = 0 To SearchArray
Set rng = Range("Y2:Y1000")
findMe = SearchArray(t)
For Each rng In rng
With rng
If LCase(rng.Value) Like "*" & LCase(findMe) & "*" Then
If Not rng Is Nothing Then
For i = 1 To Len(rng.Value)
sPos = InStr(i, UCase(rng.Value), UCase(findMe))
sLen = Len(findMe)
If (sPos <> 0) Then
rng.Characters(Start:=sPos,
Length:=sLen).Font.Color = RGB(255, 0, 0)
i = sPos + Len(findMe) - 1
End If
Next i
End If
End If
End With
Next rng
Next t
Application.EnableEvents = True
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
For Each rng In rng With rng
. It seems to incorrect loop. Normally, it should be For Each variable(rngvar) in Collection(rng) – SJN Oct 21 at 12:33For t = 0 To SearchArray
. – BigBen Oct 21 at 12:36