Optimize "For Each" routine to fill cells

SY24

New Member
Joined
Oct 6, 2023
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Greetings,

we use semi-huge parts lists in our company and I'd like to highlight certain parts in yellow. While I do have working VBA code, it operates kinda slowly the bigger our files get (between 200 to 500 rows).

Here's an example:
Assembly.xlsx
ABCDE
1Superstructure Name
2LevelPos.Asm. No.Asm. NameAmount
31.........1Fan1,000
4.2........1Blades3,000
5.2........2Motor1,000
6.2........3Housing1,000
7.2........4Oscillator1,000
8.2........5Screws15,000
9.2........6Base1,000
10.2........7Switch1,000
11.2........8Power Chord1,000
12.2........9Grille1,000
Sheet1
VBA Code:
Dim rCell As Range

    For Each rCell In Range("D:D")
        If rCell.Value Like "*Motor*" Then
            rCell.Interior.ColorIndex = 6
        ElseIf rCell.Value Like "*Oscillator*" Then
            rCell.Interior.ColorIndex = 6
        ElseIf rCell.Value Like "*Switch*" Then
            rCell.Interior.ColorIndex = 6
        ElseIf rCell.Value Like "*Button Panel*" Then
            rCell.Interior.ColorIndex = 6

        End If
    Next
Assembly.xlsx
ABCDE
1Superstructure Name
2LevelPos.Asm. No.Asm. NameAmount
31.........1Fan1,000
4.2........1Blades3,000
5.2........2Motor1,000
6.2........3Housing1,000
7.2........4Oscillator1,000
8.2........5Screws15,000
9.2........6Base1,000
10.2........7Switch1,000
11.2........8Power Chord1,000
12.2........9Grille1,000
Sheet1

Not all options defined in the code get used in every parts list, but that's ok.

I read a lot of articles and forums for optimizing this routine (many saying that For Each is the slowest), but failed to understand how to implement their solutions to my code. So I am lookin for an efficient and faster operating routine that also lets me add new options whenever needed.

Any help in regards to the problem above is greatly appreciated!

SY24
 
After trying multiple solutions, I decided to use the one from @Peter_SSs and modify that by the suggestions from @Flashbond and @Muhammad_Usman, while moving the extra column into an external worksheet:
VBA Code:
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ActiveSheet.DisplayPageBreaks = False

Dim RX As Object
Dim a As Variant, b As Variant
Dim i As Long, k As Long

Dim wks As Excel.Worksheet
    Set wks = Worksheets("ActiveSheet")

Dim wkboption As Workbook
    Set wkboption = Workbooks.Open("C:\Path\to\external\Workbook.xlsx")
Dim wksoption As Worksheet
    Set wksoption = wkboption.Worksheets("ExternalSheet")

    Set RX = CreateObject("VBscript.RegExp")
    RX.Pattern = Join(Application.Transpose(wksoption.Range("A2", wksoption.Range("A" & Rows.Count).End(xlUp)).Value), "|")
    With wks.Range("D3", wks.Range("D" & Rows.Count).End(xlUp))
        a = .Value
        ReDim b(1 To UBound(a), 1 To 1)
        For i = 1 To UBound(a)
            If RX.test(a(i, 1)) Then
                b(i, 1) = 1
                k = 1
            End If
        Next i
        If k > 0 Then
            .Value = b
            .SpecialCells(xlConstants).Interior.ColorIndex = 6
            .Value = a
        End If
    End With

    wkboption.Close SaveChanges:=False
While opening an external sheet isn't that fast, it still operates faster than my initial For Each routine. Thanks everyone for the input.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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