update the code to work in the fastest way possible

sr1111

New Member
Joined
Sep 2, 2022
Messages
46
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
I got the below code while searching in MrExcel.
My request is to update the code to work in the fastest way possible. I have many thousands of rows of data and it is taking hours.
The code should not work in the sheet "Secret code". Please help.

VBA Code:
Sub blah2()
    For Each cll In Sheets("USA").UsedRange.Cells
        myPhrase = Trim(cll.Value)
        phraseLength = Len(myPhrase)
        For j = phraseLength To 4 Step -1
            For i = 1 To phraseLength - 3
                myNewPhrase = Trim(Mid(myPhrase, i, j))
                myNewPhraselength = Len(myNewPhrase)
                If myNewPhraselength > 0 Then
                    clr = cll.Font.Color
                    For Each sht In ThisWorkbook.Sheets
                        If sht.Name <> "USA" Then
                            For Each celle In sht.UsedRange
                                x = InStr(1, celle.Value, myNewPhrase, vbTextCompare)
                                If x > 0 Then
                                    celle.Characters(Start:=x, Length:=myNewPhraselength).Font.Color = clr
                                    Do
                                        x = InStr(x + 1, celle.Value, myNewPhrase, vbTextCompare)
                                        If x > 0 Then celle.Characters(Start:=x, Length:=myNewPhraselength).Font.Color = clr
                                    Loop Until x = 0
                                End If
                            Next celle
                        End If
                    Next sht
                End If
            Next i
        Next j
    Next cll
End Sub
 
Solution would be a 'smarter' For loop structure. What that would be is beyond my thinking at this point in time. :(
 
Last edited:
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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