Excel vba - 查找列号,其中列数据(多个子句) [英] Excel vba - find row number where colum data (multiple clauses)

查看:525
本文介绍了Excel vba - 查找列号,其中列数据(多个子句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是Excel示例:

  ** ABCD ** 
1 id1 day1 val1 xxx
2 id2 day1 val2 xxx
3 id3 day1 val3 xxx
4 id1 day2 val1 xxx
5 id2 day2 val2 xxx
6 id3 day2 val3 xxx

我需要找到行号(在这种情况下,行号是 2 ),其中B =day1,而A =id2。



根据行号,我需要进一步得到其他列的值,即C2,D2



希望问题清楚。



谢谢!

解决方案

您可以使用MATCH函数获取行号:

  = MATCH(1,INDEX(($ A $ 1:$ A $ 6 =id2)*($ B $ 1:$ B $ 6 =day1),),0)

如果这些条件没有匹配项,公式将返回#N / A错误。您还可以将条件更改为单元格引用,例如:

  = MATCH(1,INDEX(($ A $ 1: $ A $ 6 = F1)*($ B $ 1:$ B $ 6 = G1),)0)

对于您的问题的第二部分,返回值与找到的行号,您可以使用INDEX函数从列返回值。所以假装匹配公式在单元格H1中,这两个公式将分别从列C和D中返回值:

  = INDEX ($ C $ 1:$ C $ 6,H1)
= INDEX($ D $ 1:$ D $ 6,H1)

或者你可以把它全部放在一个公式中:

  = INDEX($ C $ 1: $ C $ 6,MATCH(1,INDEX(($ A $ 1:$ A $ 6 = F1)*($ B $ 1:$ B $ 6 = G1),0))
如果您不想查看错误,可以在excel 2007 +上使用IFERROR




$ b $($ A $ 1 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ B $ 6 = G1),),0)),无匹配)

错误检查Excel 2003及以下版本:

  = IF(ISNA(MATCH(1,INDEX(($ A $ 1:$ A $ 6 = F1 )*($ B $ 1:$ B $ 6 = G1),),0)),无匹配,INDEX($ C $ 1:$ C $ 6,MATCH(1,INDEX(($ A $ 1:$ A $ 6 = F1)*($ B $ 1:$ B $ 6 = G1),),0)))

:我每个用户请求都包含一个VBA解决方案。这使用find循环,这是非常有效和灵活的,并显示如何找到匹配后,从其他列提取值:

  Sub tgr()

Dim rngFound As Range
Dim strFirst As String
Dim strID As String
Dim strDay As String

strID =id2
strDay =day1

设置rngFound =列(A)Find(strID,Cells(Rows.Count,A),xlValues, xlWhole)
如果没有rngFound是没有
strFirst = rngFound.Address
Do
如果LCase(Cells(rngFound.Row,B)。Text)= LCase(strDay )然后
'找到一个匹配
MsgBox找到一个匹配:& rngFound.Row& Chr(10)& _
C列中的值& Cells(rngFound.Row,C)。Text& Chr(10)& _
D列中的值&单元格(rngFound.Row,D)。文本
结束If
设置rngFound =列(A)Find(strID,rngFound,xlValues,xlWhole)
循环while rngFound。地址<> strFirst
End If

设置rngFound = Nothing

End Sub


I need a function in VBA that finds the row number based on 2 where clauses.

Here is the Excel sample:

**     A      B      C      D**
  1    id1    day1   val1   xxx
  2    id2    day1   val2   xxx
  3    id3    day1   val3   xxx
  4    id1    day2   val1   xxx
  5    id2    day2   val2   xxx
  6    id3    day2   val3   xxx

I need to find the row number (in this case row number is 2) where B = "day1" and A = "id2".

Based on the row number, I need to further get the values of other columns, i.e. C2, D2

Hope that the question is clear.

Thank you!

解决方案

With your data setup like that, you can use the MATCH function to get the row number:

=MATCH(1,INDEX(($A$1:$A$6="id2")*($B$1:$B$6="day1"),),0)

If there are no matches for those criteria, the formula will return an #N/A error. You can also change the criteria to be cell references, for example:

=MATCH(1,INDEX(($A$1:$A$6=F1)*($B$1:$B$6=G1),),0)

For the second part of your question, returning values with the found row number, you can use the INDEX function to return a value from a column. So pretending the Match formula is in cell H1, these two formulas will return the value from column C and D respectively:

=INDEX($C$1:$C$6,H1)
=INDEX($D$1:$D$6,H1)

Alternately, you could put it all into a single formula:

=INDEX($C$1:$C$6,MATCH(1,INDEX(($A$1:$A$6=F1)*($B$1:$B$6=G1),),0))

And if you don't want to be looking at errors, you can use an IFERROR on excel 2007+

=IFERROR(INDEX($C$1:$C$6,MATCH(1,INDEX(($A$1:$A$6=F1)*($B$1:$B$6=G1),),0)),"No Matches")

Error checking for Excel 2003 and below:

=IF(ISNA(MATCH(1,INDEX(($A$1:$A$6=F1)*($B$1:$B$6=G1),),0)),"No Matches",INDEX($C$1:$C$6,MATCH(1,INDEX(($A$1:$A$6=F1)*($B$1:$B$6=G1),),0)))

[EDIT]: I am including a VBA solution per user request. This uses a find loop, which is very efficient and flexible, and shows how to extract values from other columns once a match has been found:

Sub tgr()

    Dim rngFound As Range
    Dim strFirst As String
    Dim strID As String
    Dim strDay As String

    strID = "id2"
    strDay = "day1"

    Set rngFound = Columns("A").Find(strID, Cells(Rows.Count, "A"), xlValues, xlWhole)
    If Not rngFound Is Nothing Then
        strFirst = rngFound.Address
        Do
            If LCase(Cells(rngFound.Row, "B").Text) = LCase(strDay) Then
                'Found a match
                MsgBox "Found a match at: " & rngFound.Row & Chr(10) & _
                       "Value in column C: " & Cells(rngFound.Row, "C").Text & Chr(10) & _
                       "Value in column D: " & Cells(rngFound.Row, "D").Text
            End If
            Set rngFound = Columns("A").Find(strID, rngFound, xlValues, xlWhole)
        Loop While rngFound.Address <> strFirst
    End If

    Set rngFound = Nothing

End Sub

这篇关于Excel vba - 查找列号,其中列数据(多个子句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆