在excel中查找满足多个条件的单元格地址

问题描述 投票:0回答:1

亲爱的excel专家,

我想找到满足几种条件的某个细胞的地址。

例如:

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

在上面的表格上,

条件1是日期。条件2是类型。

通过使用这两个条件,我需要找到相应名称的地址。

例如:

条件1:2018-01-01条件2:结果:约翰

你能给我一些建议吗?

非常感谢你。

arrays excel
1个回答
0
投票

所以假设你的数据是这样的:

  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 ???

然后下面的VBA代码就可以工作了(我已经对每行代码进行了解释,以解释发生了什么。)不知道你对VBA有多自信,如果太过分,那就道歉了。这样做可能有一种更优雅的方式,但是根据您的数据结构,我发现在一个公式中很难做到....

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
© www.soinside.com 2019 - 2024. All rights reserved.