VBA code help with multiple conditions after find

BalloutMoe

Board Regular
Joined
Jun 4, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hello All, I am looking for some help. I have the below code that finds the Employee name and Day and loops through them to let me know who left early. However some guys clock out and clock back in (Lunch break) I would like to take that into account. Any way of adding this in or any ideas of how to go about it? Thank you

Time.png

For example it will say (Nathan Oglesby took a lunch break from 1:49:27 PM to 2:20:18 PM and left at 6:00:00 PM)
or Trent Taylor took a lunch break from 12:54:15 PM to 1:28:26 PM and left at 6:00:00 PM

I hope I am clear. Thank you

VBA Code:
Sub TestFindAll()
   
    Dim SearchRange As Range
    Dim FindWhat As Variant
    Dim FoundCells As Range
    Dim FoundCell As Range
    Dim LastRowA As Long, LastRowJ As Long
    Dim WS1 As Worksheet
   
    Set WS1 = ThisWorkbook.Worksheets("DailyTimeSheet")
        LastRowJ = WS1.Range("J" & WS1.Rows.Count).End(xlUp).Row
        Debug.Print LastRowJ
       
    Dim firstAddress As String
   
    With ws
        Dim tbl As ListObject: Set tbl = .Range("DailyTime").ListObject
        Set SearchRange = tbl.ListColumns("EmployeeName").Range
    End With
   
    For t = 2 To LastRowJ
    FindWhat = WS1.Range("J" & t)
    Set FoundCells = SearchRange.Find(What:=FindWhat, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows)
                           
        If Not FoundCells Is Nothing Then
            firstAddress = FoundCells.Address
            Debug.Print "Found " & FoundCells.Value & " " & FoundCells.Offset(0, 2).Value
           
            Do
                If Not FoundCells.Offset(0, 2).Value = "Sat" And FoundCells.Offset(0, 5).Value < TimeValue("18:00:00") Then
                Debug.Print FoundCells.Value & " left early on " & FoundCells.Offset(0, 2) & " at " & TimeValue(Format(FoundCells.Offset(0, 5).Value, "hh:mm:ss"))
                End If
            Set FoundCells = SearchRange.FindNext(FoundCells)
            Debug.Print "Found " & FoundCells.Value & " " & FoundCells.Offset(0, 2)
            Loop While Not FoundCells Is Nothing And FoundCells.Address <> firstAddress
        End If
                           
Next

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Just a formula in a new column:
MrExcelPlayground5.xlsx
ABCDEFGH
1NameLocationDayDateInOuthoursLeave Early?
2Johna11/1/20218:00:00 AM6:00:00 PM10No
3Freda11/1/20218:00:00 AM5:30:00 PM9.5Yes
4Marya11/1/20218:00:00 AM6:00:00 PM10No
5Johna11/2/20218:00:00 AM6:00:00 PM10No
6Freda11/2/20218:00:00 AM6:00:00 PM10No
7Marya11/2/20218:00:00 AM5:00:00 PM9Yes
8Johna11/3/20218:00:00 AM12:00:00 PM4No
9Marya11/3/20218:00:00 AM6:00:00 PM10No
10Johna11/3/20211:00:00 PM6:00:00 PM5No
11Freda11/3/20218:00:00 AM6:00:00 PM10No
Sheet2
Cell Formulas
RangeFormula
G2:G11G2=(F2-E2)*24
H2:H11H2=IF(MAX(INDEX(FILTER($A$2:$F$11,($A$2:$A$11=A2)*($D$2:$D$11=D2)),,6))<0.75,"Yes","No")
 
Upvote 0
Just a formula in a new column:
MrExcelPlayground5.xlsx
ABCDEFGH
1NameLocationDayDateInOuthoursLeave Early?
2Johna11/1/20218:00:00 AM6:00:00 PM10No
3Freda11/1/20218:00:00 AM5:30:00 PM9.5Yes
4Marya11/1/20218:00:00 AM6:00:00 PM10No
5Johna11/2/20218:00:00 AM6:00:00 PM10No
6Freda11/2/20218:00:00 AM6:00:00 PM10No
7Marya11/2/20218:00:00 AM5:00:00 PM9Yes
8Johna11/3/20218:00:00 AM12:00:00 PM4No
9Marya11/3/20218:00:00 AM6:00:00 PM10No
10Johna11/3/20211:00:00 PM6:00:00 PM5No
11Freda11/3/20218:00:00 AM6:00:00 PM10No
Sheet2
Cell Formulas
RangeFormula
G2:G11G2=(F2-E2)*24
H2:H11H2=IF(MAX(INDEX(FILTER($A$2:$F$11,($A$2:$A$11=A2)*($D$2:$D$11=D2)),,6))<0.75,"Yes","No")
Thank you James I tried it and it works, however I need multiple more conditions for it. Because Saturdays they close early and each location has a different time. Anyway around that? So for example
if shopname is Burleson and the Day or Date is a Saturday then they close at 2 rather then 6 and so on
 
Upvote 0
The filter formula can get complicated...

This part:
($A$2:$A$11=A2)*($D$2:$D$11=D2)
is where the filtering is. You can see I have it checking the name (A2) and the date (D2). Multiplying it together is and "AND" operation. the "OR" operation can be achieved by adding the conditions together. Essentially 0=false and anything else is true. So, using the WEEKDAY function and checking the location with a series of * or + operators and clever use of parenthesis will do the trick.

If you run into trouble, post some data using xl2bb and get all of the rules down.
 
Upvote 0
I am more familiar with vba then formulas, but I’ll give it a go
 
Upvote 0
The filter formula can get complicated...

This part:
($A$2:$A$11=A2)*($D$2:$D$11=D2)
is where the filtering is. You can see I have it checking the name (A2) and the date (D2). Multiplying it together is and "AND" operation. the "OR" operation can be achieved by adding the conditions together. Essentially 0=false and anything else is true. So, using the WEEKDAY function and checking the location with a series of * or + operators and clever use of parenthesis will do the trick.

If you run into trouble, post some data using xl2bb and get all of the rules down.
Or can I return the times they left so let’s say lunch break from 1 to 2 and if they left early? Instead of yes or no. The whole goal here is to have notes ready for the user that will tell them hey employee xxx left from this time to this time for lunch and left early that day.
 
Upvote 0
This extra bit tells how many times a person left during the day.
MrExcelPlayground5.xlsx
ABCDEFGHI
1NameLocationDayDateInOuthoursLeave Early?How many times left during the day
2Johna11/1/20218:00:00 AM6:00:00 PM10No0
3Freda11/1/20218:00:00 AM5:30:00 PM9.5Yes0
4Marya11/1/20218:00:00 AM6:00:00 PM10No0
5Johna11/2/20218:00:00 AM6:00:00 PM10No0
6Freda11/2/20218:00:00 AM6:00:00 PM10No0
7Marya11/2/20218:00:00 AM5:00:00 PM9Yes0
8Johna11/3/20218:00:00 AM12:00:00 PM4No1
9Marya11/3/20218:00:00 AM6:00:00 PM10No0
10Johna11/3/20211:00:00 PM6:00:00 PM5No1
11Freda11/3/20218:00:00 AM6:00:00 PM10No0
Sheet2
Cell Formulas
RangeFormula
G2:G11G2=(F2-E2)*24
H2:H11H2=IF(MAX(INDEX(FILTER($A$2:$F$11,($A$2:$A$11=A2)*($D$2:$D$11=D2)),,6))<0.75,"Yes","No")
I2:I11I2=ROWS(FILTER($A$2:$A$11,($A$2:$A$11=A2)*($D$2:$D$11=D2)))-1
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top