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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You have several loops that involve accessing the data. That will slow things down.

Can you supply some test data via XL2BB that we can test with?
 
Upvote 0
You have several loops that involve accessing the data. That will slow things down.

Can you supply some test data via XL2BB that we can test with?

partial color vba 10_6_2022.xlsm
ABCDEFG
1soluble , in waterruleapplicableChemistrylets seehow things: go
2soluble , in waterruleapplicableChemistrylets seehow things: go
3soluble , in waterruleapplicableChemistrylets seehow things: go
4soluble , in waterruleapplicableChemistrylets seehow things: go
5
6
7
8
9
USA


partial color vba 10_6_2022.xlsm
ABCDEF
1
2
3 solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution, solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution. solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution
4
5
6
7 solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution, solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution. solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution
8
9
10
11
12
13
14 solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution, solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution. solubility rules are applied here. I have a rule. Applicability of chemistry solution. Applicability of chemistry solution
Sheet1


partial color vba 10_6_2022.xlsm
ABCDEFGHIJKLMNOPQ
1solubility rules are applied here
2Applicability of chemistry solution
3lets see if things: go as per ruler
4solubility rules are applied heresolubility rules are applied heresolubility rules are applied here
5Applicability of chemistry solutionApplicability of chemistry solutionApplicability of chemistry solution
6lets see if things: go as per rulerlets see if things: go as per rulerlets see if things: go as per ruler
7
8
9solubility rules are applied here
10Applicability of chemistry solution
11lets see if things: go as per ruler
12
13solubility rules are applied here
14Applicability of chemistry solution
15lets see if things: go as per ruler
16
17
18
19solubility rules are applied here. I have a rulesolubility rules are applied here
20Applicability of chemistry solution
21lets see if things: go as per ruler
22
Sheet3
 
Upvote 0
One can run the above VBA in the below sheets to check the expected results. I have copied the VBA and the demo sheets from the same link.
 
Upvote 0
Do you have a lot of blank cells in your data ranges?
 
Upvote 0
From what I can see, the for loops are not optimized. They are coloring the same letters multiple times.

For example the first color change happens when your string = "solub", then later on the string = "solu" and it colors again the matches that were "solub" because it contains "solu".

After that the string becomes "olub" and again colors the same "solub" because it contains "olub"

This explains why the script is taking a long time.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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