I want to compare two columns in excel if another two columns are matching.
A B C D
$10 1234D $40 100D
$20 1235D $10 1234D
$30 122D $20 1235D
$40 1222D $30 1222D
First I need to compare col A and C If any matches find in col C then I need to compare B and D are matching. Example I have $10 in A and its ID is 1234D.i need to compare the same value in C. If I found $10 in C but its id is not 1234D I need to show that un matching one in another column.
I can match A and C as below .but I'm confusing on how to compare B and D after that?i'm new to excel vba and appreciate if any one help me to do this.
Function Find_Matches()
Dim CompareRange As Variant, SelectionRange As Variant, x As Variant, y As Variant
' compare the selection.
Sheets("Menu").Activate
Set SelectionRange = Range("A2:A6")
Set CompareRange = Range("C2:C6")
' Loop through each cell in the selection and compare it to
' each cell in CompareRange.
For Each x In SelectionRange
For Each y In CompareRange
If x = y Then x.Offset(0, 4) = True
Next y
Next x
End Function
Use this macro:
it writes result (
True
orFalse
in columnE
).Explanation:
E2:E6
array formula=ISNUMBER(MATCH($A$2:$A$6 & "|" & $B$2:$B$6,$C$2:$C$6 & "|" & $D$2:$D$6,0))
- it returnsTrue
if we found, sayA2
andB2
in any row of rangeC2:D6
, e.g.C3
andD3
..Value = .Value
part rewrites formulas with theirs resultsA
andB
and searches result in concatenation of columnsC
andD
.& "|" &
in formula? Imagine following situaltion:technically, concatenation of
A1 & B1
andC1 & D1
gives you the same result:$101234D
, but we clearly see that there is no match. That's why I'm using|
as delimeter when concatenating values:A1 & "|" & B1
returns$101|234D
andC1 & "|" & D1
returns$10|1234D
and they are not the same, as we need it.Create a new column in F that is a concatenation of A & B. Do the same in column G to concatenate the values in C & D.
Then use a formula that uses VLookup to see if looking up the concatenation of A & B returns an 'N/A' error. If this is false, you have a matching column.
So, the formula for F2 is
the formula for G2 is
and the formula to do your vlookup is
If it needs to be in vba, you could set up a macro to perform these steps.