Help with Macro to define a variable range in cell AA:AA

gavhep

New Member
Joined
Nov 30, 2013
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code which searches for keywords and highlights them. The problem I have is that the macro searches the whole spreadsheet, which when searching for lots of keywords, can take a long long time. I know that my keywords will only occur in column AA:AA and this range will be variable dependant on what the user pastes.

Is there a way to speed up my macro by asking it to search column AA:AA only, and (if it speeds up and more) only search the populated cells in AA:AA?

Code below. Thank you in advance.

Sub HighlightKeywords()
Dim X As Long, N As Long, L As Long, LKW As Long, Cell As Range
Dim Keywords As String, Txt As String
Dim KW() As String, Temp() As String
Keywords = "First word|second|third|fourth one"
KW = Split(Keywords, "|")
Application.ScreenUpdating = False
For Each Cell In ActiveSheet.UsedRange
Txt = Cell.Value
For X = 0 To UBound(KW)
LKW = Len(KW(X))
If InStr(1, Txt, KW(X), vbTextCompare) Then
Temp = Split(Txt, KW(X), , vbTextCompare)
L = 0
For N = 0 To UBound(Temp) - 1
L = L + Len(Temp(N))
Cell.Characters(L + 1, LKW).Font.Bold = True
Cell.Interior.Color = vbYellow
L = L + LKW
Next
End If
Next
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi gavhed,

if the values you search are strings/constants you may try and replace
Code:
For Each Cell In ActiveSheet.UsedRange
with
Code:
For Each Cell In ActiveSheet.Range("AA:AA").SpecialCells(xlCellTypeConstants, 23)
Code will error if there are no cells with constants found.

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top