简体   繁体   中英

Finding an cell address meeting multiple conditions in excel

Dear Experts of excel,

I would like to find an address of certain cell which meet several condition.

For example:

name       date     type   date     type    date    type
John    2018-01-01  b   2018-03-01  d   2018-02-01  a
Kyle    2018-01-01  a   2018-02-01  b   2018-04-01  b
Kate    2018-02-01  c   2018-01-01  d   2018-05-01  c
Mattew  2018-01-01  c   2018-03-01  a   2018-06-01  d

On the above sheet,

Condition 1 is Date. Condition 2 is Type.

By using those two conditions I need to find the address of corresponding name.

For example:

Cond.1: 2018-01-01 Cond.2: a Result: John

Could you give me some advice?

Thank you very much.

So assuming your data is set out like this:

  A       B            C      D            E       F           G
1 name    date         type   date         type    date        type
2 John    2018-01-01   b      2018-03-01   d       2018-02-01  a
3 Kyle    2018-01-01   a      2018-02-01   b       2018-04-01  b
4 Kate    2018-02-01   c      2018-01-01   d       2018-05-01  c
5 Mattew  2018-01-01   c      2018-03-01   a       2018-06-01  d
6
7 Date   2018-01-01
8 Type   a
9 Result ???

Then the following VBA code would work, (I've put an explanation on each line of code to explain what is happening.) Don't know how confident you are with VBA so apologies if it's too much. There is probably a much more elegant way of doing this, but with the structure of your data I found it difficult to do in one formula....

Sub IdentifyName()

Dim condition1 As Variant '' date
Dim condition2 As Variant '' type
Dim rng As Range '' where the data is
Dim cel As Range
Dim cel2 As Range
Dim rownum As Variant '' what row the matched date lies on
Dim rownum2 As Variant '' what row the matched type lies on
Dim colnum As Variant '' what column the matched date lies in
Dim colnum2 As Variant '' what column the matched type lies in

Set rng = Range("A2:G5") '' the table where the data is ** Change this to set the range where you search for your data **

condition1 = Cells(7, 2).Value ''condition 1 (date)

condition2 = Cells(8, 2).Value '' condition 2 (type)

For Each cel In rng.Cells '' create loop for every cell in the above declared range

    If cel.Value = condition1 Then '' if the cell matches the condition 1

        rownum = cel.row '' then find the row number of the matched cell

        colnum = cel.Column '' find the column number of the matched cell

            For Each cel2 In rng.Cells '' if we have a match on date create loop for every cell in the same range for condition2

                If cel2.Value = condition2 Then '' if the cell in the range matches condition2

                    rownum2 = cel2.row '' identify the row number of the matched cell

                    colnum2 = cel2.Column '' identify the column number of the matched cell

                        If rownum = rownum2 And colnum = colnum2 - 1 Then '' if both the matches are on the same row and condition2 is 1 column to the right

                            Cells(9, 2).Value = Cells(rownum, 1).Value '' put the name in the cell on the corresponding row
                        End If
                End If
            Next cel2 '' if the conditions arent met then try the next cell in range to match condition 2
    End If
 Next cel '' if the conditions arent met then try the next cell in range to match condition 1

End Sub

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