Array with If Then

papadogiannakis

New Member
Joined
Nov 22, 2018
Messages
3
Hello, I have a slight problem.

I want a macro that can check if a value on sheet 1, column B matches one of multiple values in sheet 2, column B on and if so make the cell on the first sheet turn green.

I need this to compare batch codes and to quickly see what ones have been approved.
Only I have no idea where to start. The most I have worked with VBA is to create macro's that print things, haha.

I did some research and i think i need the Array functions and the If-Then function.

This is what i came up with, but it doesnt work.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Range("B8: B30")
    If cell.Value = Sheets("Batch Data").Range("A2:A30") Then
    cell.Interior.Color = XlRgbColor.rgbLightGreen
End If
Next

End Sub

As you can see I am really new at this and was made the 'computer guy' because i game a bit, haha
So help and explanation is greatly needed and appreciated!

Many thanks in advance!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi & welcome to MrExcel.
You can do this with conditional formatting.
Select B8:B30 > Home Tab > Conditional format . New Rule > Use a formula > =ISNUMBER(MATCH(B8,'Batch Data'!A$2:A$30,0)) > select format > Ok
 
Upvote 0
Well, that works! Kind of. Had te replace the comma's with whatever these ' ; ' are called in English :)
Just out of curiosity, would it be possible to do it with a macro?
Even if I don't understand much about VBA, I do want to learn!

Thanks for the quick reply and help!
 
Upvote 0
How about
Code:
Sub ColourCells()
   Dim Cl As Range
   Dim Res As Variant
   
   For Each Cl In Range("B8:B30")
      Res = Application.Match(Cl.Value, Sheets("Batch Data").Range("A2:A30"), 0)
      If Not IsError(Res) Then
         Cl.Interior.Color = XlRgbColor.rgbLightGreen
      Else
         Cl.Interior.Color = xlNone
      End If
   Next Cl
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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