If a list of cell Values do not match a value within a range, highlight the cell

APInfa

New Member
Joined
Aug 2, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am creating a system which reads all existing unit of measurements (UOM) and checks the data I am trying to enter. If the new data has a UOM that doesn't match I would like to highlight it. Here is the code I am using but it's giving me a mismatch error on my main "if" statement:

Sub Match()

Dim cells1 As Range
Dim cell As Range
Set cells1 = Range("D3", "D1000") ' Data I am trying to check
Dim Check As Range
Set Check = ThisWorkbook.Worksheets("Sample Code (Don't Alter)").Range("D7", "D1000") 'UOM Data Storage

'Loop through each cell in your selected range looking for highlight
For Each cell In cells1


'will highlight the cells that don't match the existing UOM interface
If Not cells1.Value = Check.Value Then
cell.Interior.ColorIndex = 20 'HERE IS THE MISMATCH ERROR

End If

Next cell
MsgBox "All UOM That don't match have been marked"

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi & welcome to MrExcel.
How about
VBA Code:
Sub APInfa()
   Dim Cl As Range
   Dim Dic As Object
   
   With CreateObject("Scripting.dictionary")
      For Each Cl In Sheets("Sample Code (Don't Alter)").Range("D7:D1000")
         If Cl.Value <> "" Then .Item(Cl.Value) = Empty
      Next Cl
      For Each Cl In ActiveSheet.Range("D3:D1000")
         If Cl.Value <> "" And Not .Exists(Cl.Value) Then
            Cl.Interior.ColorIndex = 20
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
What about using Conditional Formatting?

VBA Code:
Sub HighlightNonMatches()
  With Range("D3:D1000")
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(D3<>"""",ISNA(MATCH(D3,'Sample Code (Don''t Alter)'!D$7:D$1000,0)))"
    .FormatConditions(1).Interior.ColorIndex = 20
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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