Formatting VBA - Why isn't it working on all of them?

cjlittlet

New Member
Joined
Dec 2, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have the following VBA that is supposed to highlight the words in col C that are in col A but it only works on 90% of the cells. The col's match but for some reason, they get skipped. You can see on line 12 it skipped it entirely but on 11 & 13 it worked. Same with lines 17 & 23. I've tried taking out all spaces, clearing all formats first, and copying and pasting into a new workbook and I just can't figure out why it's skipping some of these.

Sub WordCompare()
Dim Cl As Range
Dim x As Long

For Each Cl In Range("C1", Range("C" & Rows.Count).End(xlUp))
x = 0
If Cl.Offset(, -2).value <> "" Then x = InStr(1, Cl.value, Cl.Offset(, -2).value, vbTextCompare)
If x > 0 Then Cl.Characters(x, Len(Cl.Offset(, -2))).Font.Color = vbRed
Next Cl
End Sub



Book1
ABC
55280 SOLUTIONS LLC5280 SOLUTIONS LLC IPACESETTERS LLC TULSA S GREEN COUNTRY STAFFING LLC
6ABSENTEE SHAWNEE TRIBE SHAWNEExABSENTEE SHAWNEE TRIBE OF OKFINANCE
7ACCENT MOVING&STORAGE INC 1992ACCENT MOVING&STORAGE INC 1992 SCHNEIDER NATIONAL CARRIERS INC% ADP&TALX UCM SERVICES INC
8ACCOUNTABILITY PARTNERS LLCFEDERAL EXPRESS CORP ACCOUNTABILITY PARTNERS LLC IPSONG LLC TAKEONE NETWORK CORP PLP ENTERTAINMENT INC FEATURES PROCESSING CA INC BABBIT MITCHELL&CHANCE PLLC
9ALMS WESTERN AUTO LLCALMS WESTERN AUTO LLC LEGEND SENIOR LIVING LLC INC
10ALORICA AT HOME LLCxALORICA INC WALMART ASSOCIATES INC
11AMERICAN AIR INCAMERICAN AIR INC NEXTEP BUSINESS SOLUTIONS IV INC HADLEY HEATING AND AIR LLC
12AMERICAN AIRLINES INC %DEBTOR INxAMERICAN AIRLINES INC
13AMERICAN DIABETES ASSOCIATIONTULSA COUNTY PUBLIC FACILITIES AUTHSHANNA DUTTON EXPOSERVE MANAGEMENT CORPORATION AMERICAN DIABETES ASSOCIATION
14BIG FIVE COMMUNITY SERVICES INCxBIG FIVE COMMUNITY SERVICE INC MINDLEAF TECHNOLOGIES INC FEDERAL STAFFING RESOURCES LLC
15CHEROKEE HILLS VETERINARY CLINICCHEROKEE HILLS VETERINARY CLINIC ROSE ROCK VETERINARY HOSPITAL NVA MEMORIAL RD VETERINARY MGMT LLC% COMPUPAY INC VIP PETCARE
16CHEROKEE NATIONCHEROKEE NATION OF OKLAHOMA LLC
17CHEROKEE NATION BUSINESSxCHEROKEE NATION ENTERTAINMENTS3 MANAGEMENT GROUP LLC
18CHEROKEE NATION ENTERTAINMENTCHEROKEE NATION ENTERTAINMENTS3 MANAGEMENT GROUP LLC G4S SECURE SOLUTIONS (USA) INC DATA EXCHANGE INCBESSY KNOX
19CHEROKEE NATION MISSION SOLUTIONSCHEROKEE NATION MISSION SOLUTIONSS3 MANAGEMENT GROUP LLC GEOFFREY COLPITTS CONSULTING LLC
20CHEROKEE NATION OF OKLAHOMACHEROKEE NATION OF OKLAHOMA LLC
21CHEROKEE TEMPS INCCAPTIVE AIRE SYSTEMS INC%ULTIMATE SOFTWARE GROUP INC CHEROKEE TEMPS INC CARSONS FOOD SERVICE MANAGEMENT LLC
22CHICKASAW NATIONCHICKASAW NATION TRIBAL GOVERNMENT% KATHY PETTITT
23CHICKASAW NATION TRIBAL GOVERNMENTxCHICKASAW NATIONDIVISION OF COMMERCE ACADEMY LTD %THOMAS&COMPANY SOMRG LLC
Sheet1
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Your macro is matching right, in cell A12 you are matching AMERICAN AIRLINES INC %DEBTOR IN which isn't present in C12 since it's 'only' AMERICAN AIRLINES INC so they can't match.
For the same reason neither do rows 6, 10, 14, 17 and 23 match.
 
Last edited:
Upvote 0
It's supposed to match whole words so AMERICAN AIRLINES would be in red but the rest wouldn't.
 
Upvote 0
See if this is closer to what you want. I note that the range you provided starts on row 5 - change that to suit.

VBA Code:
Option Explicit
Sub Highlight_Red()
    Dim Rng As Range, c As Range, myString As String, i As Long, j As Long, k As Long, xList, item
    Set Rng = Range("C5", Cells(Rows.Count, "C").End(xlUp))
    
    For Each c In Rng
        myString = c.Offset(, -2).Value
        xList = Split(myString, " ")
        For Each item In xList
            j = InStr(c, item & " ")
            If j Then c.Characters(j, Len(item) + 1).Font.Color = vbRed
        Next item
    Next c
    
End Sub
 
Upvote 0
Slight improvement on post #4

VBA Code:
Option Explicit
Sub Highlight_Red_2()
    Dim Rng As Range, c As Range, i As Long, xList, yList, itemx, itemy
    Set Rng = Range("C5", Cells(Rows.Count, "C").End(xlUp))
    
    For Each c In Rng
        xList = Split(c.Offset(, -2), " ")
        yList = Split(c, " ")
        For Each itemx In xList
            For Each itemy In yList
                If itemy & " " Like itemx & " " Or " " & itemy Like " " & itemx Then
                    For i = 1 To Len(c) - Len(itemx) + 1
                        If Mid(c, i, Len(itemx)) = itemx Then
                            c.Characters(i, Len(itemx)).Font.Color = vbRed
                        End If
                    Next i
                End If
            Next itemy
        Next itemx
    Next c
End Sub
 
Upvote 0
Slight improvement on post #4

VBA Code:
Option Explicit
Sub Highlight_Red_2()
    Dim Rng As Range, c As Range, i As Long, xList, yList, itemx, itemy
    Set Rng = Range("C5", Cells(Rows.Count, "C").End(xlUp))
   
    For Each c In Rng
        xList = Split(c.Offset(, -2), " ")
        yList = Split(c, " ")
        For Each itemx In xList
            For Each itemy In yList
                If itemy & " " Like itemx & " " Or " " & itemy Like " " & itemx Then
                    For i = 1 To Len(c) - Len(itemx) + 1
                        If Mid(c, i, Len(itemx)) = itemx Then
                            c.Characters(i, Len(itemx)).Font.Color = vbRed
                        End If
                    Next i
                End If
            Next itemy
        Next itemx
    Next c
End Sub
I apologize I didn't get the notice that you had replied! This helpful and I'm using it to run after I do the first one. The problem is that matching things like LLC or INC only causes false positives.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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