简体   繁体   中英

How to find a value in a column and get the value of another column on that row and format that row in Excel?

So my problem is,

I have 4 columns(A,B,C,D) and I need to find out if a value in column C exists in column A, if it does I need to look for the value in column D(on my initial value's row) and check if it is same with the value in column B(on the row which I've found a match) and then do this for every value in column C.

I have never written code in Visual Basic and have hardly an Excel experience. I think code should be something like this:(in VB of course)

for(int i=0;i<numOfRecordsInC;i++){
    for(int m=0;m<numOfRecordsInA;m++){
        if(C[i]==A[m] && D[i]==B[m]){//ith,mth element of the columns
        //Highlight mth row
        //or just print "1" to any column on mth row if that's easier
        }
    }
}

How can I write something like this in VBA inside Excel or can I do this with other tools in Excel? Conditional formatting for duplicates was not useful since there is no guarentee that the columns won't have duplicate values inside themselves.

Okay so after some research I've come up with a formula like this:

=IF(COUNTIF(E$5:E$1605;P5);IF(VLOOKUP(P5;E$5:J$1605;2;0)=R5;"Matched";"Not Matched");"Not Found")

But I get wrong results or there is something wrong with my formula.

According to the example I gave at the beginning; A is E, B is J, C is P, D is R A是E,B是J,C是P,D是R

Do you want something like this ?

Sub highlightCells()

With Worksheets("sheet1")
    'calculate last row for colA & colB
    lastRowLeft = WorksheetFunction.Max( _
        .Cells(.Rows.Count, "A").End(xlUp).Row, _
        .Cells(.Rows.Count, "B").End(xlUp).Row)
    'calculate last row for colC & colD
    lastRowRight = WorksheetFunction.Max( _
        .Cells(.Rows.Count, "C").End(xlUp).Row, _
        .Cells(.Rows.Count, "D").End(xlUp).Row)

    'match
    For i = 1 To lastRowRight
        For m = 1 To lastRowLeft
            If .Cells(i, "C").Value = .Cells(m, "A").Value _
            And .Cells(i, "D").Value = .Cells(m, "B").Value Then
                'do sth
                Debug.Print "(m,i)=(" & m & "," & i & ")"
            End If
        Next
    Next
End With

End Sub

Example data

 | A  B  C  D
--------------
1|       1  A
2| 5     2  B
3| 2  B  3  C
4|       4  D
5|       5  E
6| 4  D

Example result

(m,i)=(3,2)
(m,i)=(6,4)

This is tagged as VBA but you do mention 'other tools' as well.

You can also do it using index and match like this (if the data starts in row 2):-

=INDEX($B$2:$B$7,MATCH($C2,$A$2:$A$7,0))=$D2

And then put it in Conditional Formatting | New Rule | Use a formula

在此处输入图片说明

If it's possible to have duplicate values in column A any one of which could have a match between column B and the original value in column D, then you would need an array formula like this:-

=MATCH($D2,IF($A$2:$A$7=$C2,$B$2:$B$7),0)

在此处输入图片说明

Found the formula that worked in my case(still gives correct results even if a column has duplicates)

=IF(COUNTIF(E$5:E$1605;P5);IF(COUNTIFS(E$5:E$1605;P5;J$5:J$1605;R5);"Matched";"Not Matched");"Not Found")

Excel does not let me use , to seperate parameters and suggests ; . Maybe that's because of the language settings of my Windows so if you end up using this formula please keep that in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM