If two cells are the same, make them green, throught the whole column

CaptainCsaba

Board Regular
Joined
Dec 8, 2017
Messages
78
Hi!

This one probably has an easy answer but I just cant figure it out. I have two columns that need to be compared. I want to make both B2 and D2 green if they both have let's say 21345 in them. But this applies to all of the rest of the file. There are around 2000 rows and this would need to apply to all of them. so if D365 and B365 are the same they should be green.

I can't really fill down the conditional formatting tool and there I can only give the rule "= D2=B2" which would put D2=B2 to all of the cells. What should I do?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this VBA solution

Code:
Option Explicit


Sub Green()
    Dim lr As Long, i As Long
    lr = Range("B" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For i = 2 To lr
        If Range("B" & i) = Range("D" & i) Then
            Range("B" & i).Interior.ColorIndex = 4
            Range("D" & i).Interior.ColorIndex = 4
        End If
    Next i
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub


Standard Module
How to install your new code
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Press Alt+F11 to open the Visual Basic Editor
Choose Insert > Module
Edit > Paste the macro into the module that appeared
Close the VBEditor
Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


To run the Excel VBA code:
Press Alt-F8 to open the macro list
Select a macro in the list
Click the Run button
 
Upvote 0
Hey alansidman!

Thank you for the code. I did how you instructed. If I get terror code '13' Type mismatch, what did I do wrong?
 
Last edited:
Upvote 0
I can't really fill down the conditional formatting tool and there I can only give the rule "= D2=B2" which would put D2=B2 to all of the cells. What should I do?
You certainly can use Conditional formatting.
Select B2:B?? and apply CF, use a formula .. =AND(B2<>"",B2=D2) and set your formatting, OK. Excel will automatically adjust for the other row numbers.

Then do the same thing with D2:D?? & exactly the same formula

Excel Workbook
BCD
1
22134521345
394
446
5569569
6
722
865896859
CF Matches
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B21. / Formula is =AND(B2<>"",B2=D2)Abc
D21. / Formula is =AND(B2<>"",B2=D2)Abc
 
Last edited:
Upvote 0
I get terror code '13' Type mismatch, what did I do wrong?

When you get the error and click on debug, what line of code is highlighted?
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,922
Members
449,056
Latest member
denissimo

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