简体   繁体   English

Excel公式可引用多列中的多个条件

[英]Excel formula to reference multiple criteria in multiple columns

I am trying to create a formula to return back a row number in excel. 我正在尝试创建一个公式来返回excel中的行号。 Basically I have two different tables that I want to link. 基本上我有两个不同的表,我想链接。 In the employee section under the reference column, I want to place a formula that will reference a number if certain criteria is met. 在参考列下的员工部分,我想放置一个公式,如果满足某些条件,将引用一个数字。

For example, I want to be able to take 2256 (numb for tom) and search the "assign" column in the building table for a match, then look if the cell directly next to it denotes if it is overtime or not (x is overtime), I then want it to return the reference number for the corresponding row in array B3:B7 for shifts that match the number of the employee but are not overtime. 例如,我希望能够获取2256(麻木为汤姆)并搜索构建表中的“assign”列以进行匹配,然后查看紧邻它的单元格是否表示它是否超时(x是超时),然后我希望它返回数组B3中相应行的参考号:B7,用于匹配员工编号但不是加班的班次。 If it fails to find something that matches those two criteria, I need it to return the number 0. In this case I want tom to have a reference number reported of 1, liz it have a reference of 4, and kathy to show 3. Amber should remain blank. 如果它找不到符合这两个标准的东西,我需要它返回数字0.在这种情况下,我希望tom有一个报告为1的参考号,liz它的引用为4,而kathy为3。琥珀色应保持空白。

Building

Ref     Post     Start     End     assign     overtime   
 1       sh      1600      2400     2256               
 2       sn      600       1400     2057         x     
 3       sh      1000      1800     2107              
 4       sd      1400      2200     2057              
 5       dc      700       1500     2256         x


Employee

Name     Numb     Start    End     Post        Reference

tom      2256       day     eve      sh            ??

Liz      2057       day     eve      sd            ??

Amber    2952       day     eve      none          ??

kathy    2107       day     eve      sh            ??

Can someone please help with this formula? 有人可以请帮助这个公式吗? I have tried versions of sumproduct, index, match, if, and and always getting an error. 我已经尝试了sumproduct,index,match,if和的版本,并且总是收到错误。 Thanks. 谢谢。

Using your sample data, the formula would be: 使用您的样本数据,公式将是:

=IFERROR(INDEX($A$2:$A$6,MATCH(1,INDEX(($E$2:$E$6=B10)*($F$2:$F$6=""),),0)),"")

However, that only returns the Ref number of the first match. 但是,这只返回第一场比赛的参考号。 Is there ever a time when there could be more than one occurrence? 有没有一次可能出现多次? If so, what should the result be? 如果是这样,结果应该是什么?

If it can only ever have a single occurrence, perhaps a simple SUMIFS would be better for you: 如果它只能出现一次,那么简单的SUMIFS可能对您更好:

=SUMIFS($A$2:$A$6,$E$2:$E$6,B10,$F$2:$F$6,"")

And then format the sheet to not display 0s, or use a custom format to hide them: 0;0;;@ 然后将工作表格式化为不显示0,或使用自定义格式隐藏它们: 0;0;;@

Of course, as always, adjust ranges to suit 当然,和往常一样,调整范围以适应

No disrespect to the formula answer, I am sure it works well. 没有不尊重公式的答案,我相信它运作良好。 There are many solutions to your problem, mine is not necessarily the best. 你的问题有很多解决方案,我的不一定是最好的。 However... You might want to try to write a VB macro to accomplish the task. 但是......您可能想尝试编写VB宏来完成任务。 In case you are unaware, macros are a simple type of programming, learning how to use macros opens a whole world of useful possibilities in excel. 如果你不知道,宏是一种简单的编程类型,学习如何使用宏可以在excel中打开一个有用的全部可能性。 (If you have the time and inclination) With a macro you can label the variables, check the results and debug easily, as well as modify it with any future enhancements. (如果您有时间和倾向)使用宏,您可以标记变量,检查结果并轻松调试,以及使用任何未来的增强功能进行修改。 You can run a macro using the Developer-> Visual Basic sub-menu. 您可以使用Developer-> Visual Basic子菜单运行宏。 Running and debugging a macro is easy and fun...try it and see 运行和调试宏很简单有趣...试试看

Public Sub Employee_Not_Overtime_Check()
'
    Dim iMaxEmployee As Long
    Dim iMaxBuilding As Long
    Dim irow As Long
    Dim iEmpNum As Long
    Dim iReference As Integer

'   Initialisation, stop screen refresh during macro execution

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    iMaxBuilding = Sheets("Building").UsedRange.Rows.Count
    iMaxEmployee = Sheets("Employee").UsedRange.Rows.Count

'    For debug, setting limits smaller



     iMaxBuilding = 10
       iMaxEmployee = 10


'   Loop through the Employee Records, for each one check their overtime status

        irow = 2
        Do While irow <= iMaxEmployee
            Sheets("Employee").Select
            iEmpNum = Cells(irow, 2).Value

            Call CheckOvertime(iEmpNum, iMaxBuilding, iReference)

            Sheets("Employee").Select
            Cells(irow, 6).Value = iReference

            irow = irow + 1
        Loop


        Application.ScreenUpdating = True
        Application.DisplayAlerts = True


    End Sub

Function CheckOvertime(ByVal iEmpNum As Long, ByVal iMaxBuilding As Long, ByRef iReference As Integer)

    Dim irow As Long

    Sheets("Building").Select

    iReference = 0

    For irow = 2 To iMaxBuilding
        If Cells(irow, 5).Value = iEmpNum Then
            If Cells(irow, 6).Value <> "x" Then
                 iReference = Cells(irow, 1).Value
                 Exit For
            End If
        End If

    Next irow

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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