Return Each First and Last Instance of a Value in a Range

Lance1227

New Member
Joined
Mar 27, 2023
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to return the first and last instances of a value, but the issue I'm having is that there may be multiple instances of the value in the range and I need each occurrence results displayed separately. The range is for a monthly timecard and the value is time off for vacation. I'm trying to return the first and last day of each occurrence of consecutive days. So in the attached file, the first three days tagged "VL" would return the dates 4 Jan 23 in one cell and 6 Jan 23 in the next cell. Then it would go to the next occurrence and again return first and last dates. I'm seeing how to get the first and last occurrence, but it's the multiple occurrences that has me stumped. Any assistance would be most appreciated.
 

Attachments

  • Timecard VL.JPG
    Timecard VL.JPG
    47.5 KB · Views: 12
  • Timecard VL Results.JPG
    Timecard VL Results.JPG
    17 KB · Views: 13

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
See if you can adapt this to be any use for your ranges as we cannot see them.
Ensure that there is a blank column before the first date. That is column C for me. You can hide this blank column.
Ensure that there is a blank column after the last date. That is column AI for me. You can hide this blank column.

I have hidden some columns below to make the mini sheet a bit smaller for the forum.

23 05 30.xlsm
CDEFGHIJKLMNOPUVWXAHAI
1
21/01/232/01/233/01/234/01/235/01/236/01/237/01/238/01/239/01/2310/01/2311/01/2312/01/2313/01/2318/01/2319/01/2320/01/2321/01/2331/01/23
3
48VL8VL8VL8VL8VL
5
6
7StartEnd
84/01/236/01/23
911/01/2311/01/23
1020/01/2320/01/23
11  
First Last
Cell Formulas
RangeFormula
D8:D11D8=IFERROR(INDEX($2:$2,AGGREGATE(15,6,COLUMN(D$4:AH$4)/((D$4:AH$4<>"")*(C$4:AG$4="")),ROWS(D$8:D8))),"")
E8:E11E8=IF(D8="","",INDEX($2:$2,AGGREGATE(15,6,COLUMN(D$4:AH$4)/((D$4:AH$4<>"")*(E$4:AI$4="")),ROWS(D$8:D8))))
 
Upvote 0
Good evening,

Sorry for the late reply. When I posted my question, I was rushing and forgot something, so I might post a follow-up question. I'd like to see if I can figure it out by myself first though. I had to tell you how much I appreciate your response. I'm still trying to unpack it and see exactly how it works (doing this in my spare time, which I don't have a lot of), but I know I could have never come up with this. This is actually to assist another person. A new payroll system will start in a couple of months, so I am trying to automate the system as much as I can. As it stands now, she would have to manually type this information in for about 250 records each month so I will be sure to tell her how much you assisted me. She may want to sacrifice something in your honor, but since I'm against cruelty it may only be a carrot or a potato! :ROFLMAO: Seriously, I really appreciate your assistance.
 
Upvote 0
Honestly, I'm guessing I'll need your assistance again, but if I can figure it out, then it's due to you. Really appreciate you and the others giving your time and expertise to help us mere mortals.
 
Upvote 0
Thanks for your kind words. :)

Good luck with your figuring!!
 
Upvote 0
Good afternoon,

I'm wondering if I can a bit more assistance. I need to take the above one step further and link the the dates to the total number of hours and the type of exception. In the spreadsheet example below (sorry with our IT, I can't upload a mini sheet):

  • Rows 6-7 are the days of the month
  • Rows 8-10 are individual employee records (three rows for each employee)
    • Row 8 is scheduled work hours
    • Rows 9-10 are exceptions to schedule
      • Exceptions are things like vacation leave, sick leave, etc.
      • The number is the hours of exception (4SL = 4 hours Sick Leave)
      • There are approximately 30 exception codes
      • Exception codes can be between 2 and 7 letters
      • Occasionally, there are more than once exception per day, hence the two rows (9-10)
I would like the output to be the second attachment. With that, we can import the data into Access a save numerous hours manually typing the data.

Any assistance anyone can provide would be greatly appreciated.
 

Attachments

  • Time Sheet Example.JPG
    Time Sheet Example.JPG
    97.4 KB · Views: 11
  • Time Sheet Example 2.JPG
    Time Sheet Example 2.JPG
    37.7 KB · Views: 11
Upvote 0
Good afternoon,

I'm wondering if I can a bit more assistance. I need to take the above one step further and link the the dates to the total number of hours and the type of exception. In the spreadsheet example below (sorry with our IT, I can't upload a mini sheet):

  • Rows 6-7 are the days of the month
  • Rows 8-10 are individual employee records (three rows for each employee)
    • Row 8 is scheduled work hours
    • Rows 9-10 are exceptions to schedule
      • Exceptions are things like vacation leave, sick leave, etc.
      • The number is the hours of exception (4SL = 4 hours Sick Leave)
      • There are approximately 30 exception codes
      • Exception codes can be between 2 and 7 letters
      • Occasionally, there are more than once exception per day, hence the two rows (9-10)
I would like the output to be the second attachment. With that, we can import the data into Access a save numerous hours manually typing the data.

Any assistance anyone can provide would be greatly appreciated.

Hi @Lance1227,

1. Type AL Hours 10 ( Why 10 not 2? - 2AL)
2. 16 - 18 Merge Cells 2CMCL only count start 17-jan-23 stop 17-jan-23 ? Can you elaborate more for further clarification? Thank you :)
 
Upvote 0
Good afternoon,

1. Type AL Hours 10 ( Why 10 not 2? - 2AL)
I'm trying to combine as many days as possible. If the employee takes a week of sick leave, I'm hoping that could be done with one line vs one line for each day. In the previous example, I'm hoping to combine 2-3 Jan (E9:F9) into one line instead of two lines. Sometimes an employee is gone for the entire month, so would prefer that we didn't end up with 30 lines. See attached example

2. 16 - 18 Merge Cells 2CMCL only count start 17-jan-23 stop 17-jan-23 ? Can you elaborate more for further clarification?
Very sorry! 16-18 are not merged. I wasn't paying attention. The data is just too large to fit into cell 17 (T9). Only 17 has data.


I'm really sorry I wasn't clear enough. I'm working on a US military installation and the Japanese government wants to automate the Japanese payroll system for employees working on the base (believe it or not we are handwriting their payroll on ledger sized sheets now). So what I'm trying to do is automate the process as it seems pointless to go from handwriting the information to typing the information.

Again I appreciate any and all assistance
 

Attachments

  • Time Sheet Example 3.JPG
    Time Sheet Example 3.JPG
    34 KB · Views: 7
Upvote 0
Book2
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
1UNIT DESIGNATIONpREPARED BYCONTACT TEL #MANAGER'S SIGNATURECERTIFY THAT THE TIME AND ATTEN[
22JAPANESE PERSONNEL TIME
33AND ATTENDANCE RECORD (IHA)EX#BR#AND THAT OVERTIME SHOWN WAS
44LN EMP #
55Contract HoursTIME AND ATTENDANCE RECORDSWH
66Employee's NameSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTue
77LAST NAME - FIRST NAME12345678910111213141516171819202122232425262728293031
88300000010888880888888008888800888880088
99402AL8AL8SLM3SL8hp8SML2CMCL
1010MOUSE, Mickey1ND176
111230888880888888008888800888880088
122328AL8SLM8hp8SML
13Rudrud2AL5sl1ND2CMCL
Sheet1


Sheet2 Output (Output will be in sheet2, so need to add new sheet with name sheet2 before running the code)
Book2
ABCDE
1
2MOUSE, MickeyAL22-Jan-233-Jan-23
3AL83-Jan-234-Jan-23
4SLM86-Jan-237-Jan-23
5SL38-Jan-239-Jan-23
6hp811-Jan-2312-Jan-23
7ND111-Jan-2312-Jan-23
8SML814-Jan-2315-Jan-23
9CMCL217-Jan-2318-Jan-23
10RudrudAL81-Jan-232-Jan-23
11AL22-Jan-233-Jan-23
12SLM86-Jan-237-Jan-23
13sl56-Jan-237-Jan-23
14hp810-Jan-2311-Jan-23
15ND110-Jan-2311-Jan-23
16SML814-Jan-2315-Jan-23
17CMCL214-Jan-2315-Jan-23
Sheet2


First attempt, I will try to adjust as your desire output, Meanwhile you may try first either this's close what you're looking for or not.

1. Mickey 8SLM Fri 6, Why Output Start 5 Stop 6? A little bit unclear regarding start and stop. how can we define it?

VBA Code:
Option Explicit
Option Compare Text
Sub test()
Dim a As Variant
Dim rowforname%, i%, m%, j%, stringlength%, k%, result%, lr%
Dim txt As String


ReDim b(1 To 1000, 1 To 5)
a = Range("b6:ah" & Cells(Rows.Count, "b").End(xlUp).Row).Value 'Current Value Array
rowforname = 1


For i = 4 To UBound(a, 1) Step 3 'Step 3 From First
        
        For j = 3 To UBound(a, 2) 'Loop through Column
            For m = 1 To 2 'Check 2nd employee row if got existing record
            If IsNumeric(Left(a(i + m - 1, j), 1)) Then
            
                If a(i + m - 1, j) <> "" Then
          
                   stringlength = Len(a(i + m - 1, j))
                  For k = 1 To stringlength
                      If IsNumeric(Mid(a(i + m - 1, j), k, 1)) Then
                        result = result & Mid(a(i + m - 1, j), k, 1)
                      Else
                          txt = txt & Mid(a(i + m - 1, j), k, 1)
                      End If
                  Next k
              lr = lr + 1
              b(lr, 2) = txt
              b(lr, 3) = result
              b(lr, 4) = Format(CDate(a(2, j) + 44926), "DD-MMM-YY") 'Start from exact date
              b(lr, 5) = Format(CDate(a(2, j + 1) + 44926), "DD-MMM-YY") 'Stop till next date
              result = 0
              txt = ""

            End If
           End If
        Next m
    Next j
    
b(rowforname, 1) = a(i + 1, 1)
rowforname = lr + 1
Next i

Sheets("sheet2").[a2].Resize(UBound(b, 1), UBound(b, 2)).Value = b

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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