Reputation: 1
I am trying to create a formula to return back a row number in 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. 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.
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. Thanks.
Upvotes: 0
Views: 5565
Reputation: 1
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. 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. (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. 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
Upvotes: 0
Reputation: 26650
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($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;;@
Of course, as always, adjust ranges to suit
Upvotes: 2