Code to colour in cell when criteria not met

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
Hi, I need a code to look at the data in column AB and then look at column AC and colour in the cells if the criteria is not met.

Criteria:-

If AB has the data starting with A then the data in AC must also start with A, if the data in AB starts with either N or J then the data in AC must start with N and finally if the data in AB starts with V then the data in AC must start with S. So like I say if the criteria is not met then I would like the cells highlighted.

It has 35000 rows of data to look at so a code would be better than an If/And type formula. Thanks.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Yes in the corresponding row please. i.e AB36 and AC36 etc..
 
Upvote 0
Try this and see if it's your sort of thing.

Whether or not, I'm afraid I'm out for the rest of the night.
Code:
Sub hilights()
Dim lrab As Long, lrac As Long
Dim mx As Long, i As Long
Dim ab, ac, x, y
Dim ash As Worksheet
Set ash = ActiveSheet

lrab = ash.Cells(Rows.Count, "ab").End(xlUp).Row
lrac = ash.Cells(Rows.Count, "ac").End(xlUp).Row
mx = Application.Max(lrab, lrac)
ab = Range("AB1").Resize(mx)
ac = Range("AC1").Resize(mx)

For i = 1 To mx
x = Left(ab(i, 1), 1)
y = Left(ac(i, 1), 1)
flg = 0
If ((x = "A") * (y = "A")) _
    + (((x = "N") + (x = "J")) * (y = "N")) _
    + ((x = "V") * (y = "S")) Then flg = 1
If flg = 0 Then Cells(i, "ab").Resize(, 2).Interior.Color = vbYellow
Next i

End Sub
 
Upvote 0
Thanks thats sort of ok but I need it to leave alone any other cells that do not have the said criteria.
 
Upvote 0
Thanks thats sort of ok but I need it to leave alone any other cells that do not have the said criteria.
I don't see how this fits in with your initial request
So like I say if the criteria is not met then I would like the cells highlighted.
Maybe you could provide a bit of sample data showing clearly what you do want and what you don't want.
 
Upvote 0
At the moment in column AB it is looking for letters A, J, N & V then there matches listed above in column AC. If there is any other letters in column AB i.e Z, D, W etc.. then the code moves onto the next row.

I think from my limited knowledge of code when it says

Code:
If 
((x = "A") * (y = "A")) _
    + (((x = "N") + (x = "J")) * (y = "N")) _
    + ((x = "V") * (y = "S")) Then flg = 1
If flg = 0 Then Cells(i, "ab").Resize(, 2).Interior.Color = vbYellow

it then needs an Else something....
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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