macro or formulas to lookup by dates

kelvin_9

Active Member
Joined
Mar 6, 2015
Messages
483
Office Version
  1. 2019
greeting to all

i have 14days(7days/week) schedule with over 1000 employees with various working start time and end time each day in sheet1
i am looking for a macro or formula to export first 7days to sheet2 and another 7days to sheet3
also i need 2 empty rows to sepearte each start time for easy reference

i have manually do it on week1

how can i make this happens?

here is my workbookDropbox

your guidance would greatly appreciate


test.xlsx
ABCDEFGHI
1
2
3Worker TypeLOBLocationPerson No.DateNameJOBStart TimeEnd Time
401/26/2025apple8:158:45
501/26/2025apple8:1517:45
601/26/2025apple8:1517:45
701/26/2025apple8:1517:45
801/26/2025apple8:1517:45
901/26/2025apple8:1517:45
1001/26/2025apple8:1517:45
1101/26/2025apple8:3018:00
1201/26/2025apple8:3018:00
1301/26/2025apple8:3018:00
1401/26/2025apple8:3018:00
1501/26/2025apple8:3018:00
1601/26/2025apple8:3018:00
1701/26/2025apple8:3018:00
1801/26/2025apple8:3018:00
1901/26/2025apple9:0018:30
2001/26/2025apple9:0018:30
2101/26/2025apple9:0018:30
2201/26/2025apple9:0018:30
2301/26/2025apple9:0018:30
2401/26/2025apple10:1515:30
2501/26/2025apple11:3021:00
2601/26/2025apple12:3022:00
2701/26/2025apple13:1522:45
2801/26/2025apple13:1522:45
2901/26/2025apple13:1522:45
3001/26/2025apple13:1522:45
3101/26/2025apple13:1522:45
3201/26/2025apple13:1522:45
3301/26/2025apple13:1522:45
3401/26/2025apple13:1522:45
3501/26/2025apple13:1522:45
3601/26/2025apple13:1522:45
3701/26/2025apple13:1522:45
3801/26/2025apple13:1522:45
3901/26/2025apple15:3022:15
4001/27/2025peter8:158:45
4101/27/2025peter8:1517:45
4201/27/2025peter8:1517:45
4301/27/2025peter8:1517:45
4401/27/2025peter8:1517:45
4501/27/2025peter8:1517:45
4601/27/2025peter8:1517:45
4701/27/2025peter8:3018:00
4801/27/2025peter8:3018:00
4901/27/2025peter8:3018:00
5001/27/2025peter8:3018:00
5101/27/2025peter8:3018:00
5201/27/2025peter8:3018:00
5301/27/2025peter8:3018:00
5401/27/2025peter8:3018:00
5501/27/2025peter9:0018:30
5601/27/2025peter9:0018:30
5701/27/2025peter9:0018:30
5801/27/2025peter9:0018:30
5901/27/2025peter9:0018:30
6001/27/2025peter10:1515:30
6101/27/2025peter11:3021:00
6201/27/2025peter12:3022:00
6301/27/2025peter13:1522:45
6401/27/2025peter13:1522:45
6501/27/2025peter13:1522:45
6601/27/2025peter13:1522:45
6701/27/2025peter13:1522:45
6801/27/2025peter13:1522:45
6901/27/2025peter13:1522:45
7001/27/2025peter13:1522:45
7101/27/2025peter13:1522:45
7201/27/2025peter13:1522:45
7301/27/2025peter13:1522:45
7401/27/2025peter13:1522:45
7501/27/2025peter15:3022:15
7601/28/2025gary8:158:45
7701/28/2025gary8:1517:45
7801/28/2025gary8:1517:45
7901/28/2025gary8:1517:45
8001/28/2025gary8:1517:45
8101/28/2025gary8:1517:45
8201/28/2025gary8:1517:45
8301/28/2025gary8:3018:00
8401/28/2025gary8:3018:00
8501/28/2025gary8:3018:00
8601/28/2025gary8:3018:00
8701/28/2025gary8:3018:00
8801/28/2025gary8:3018:00
8901/28/2025gary8:3018:00
9001/28/2025gary8:3018:00
9101/28/2025gary9:0018:30
9201/28/2025gary9:0018:30
9301/28/2025gary9:0018:30
9401/28/2025gary9:0018:30
9501/28/2025gary9:0018:30
9601/28/2025gary10:1515:30
9701/28/2025gary11:3021:00
9801/28/2025gary12:3022:00
9901/28/2025gary13:1522:45
10001/28/2025gary13:1522:45
10101/28/2025gary13:1522:45
10201/28/2025gary13:1522:45
10301/28/2025gary13:1522:45
10401/28/2025gary13:1522:45
10501/28/2025gary13:1522:45
10601/28/2025gary13:1522:45
10701/28/2025gary13:1522:45
10801/28/2025gary13:1522:45
10901/28/2025gary13:1522:45
11001/28/2025gary13:1522:45
11101/28/2025gary15:3022:15
list




test.xlsx
ABCDEFGHIJKLMNOP
1
2
3
4
5
6
7
801/26/202501/27/202501/28/202501/29/202501/30/202501/31/202502/01/2025
98:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
108:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
118:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
128:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
138:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
148:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
158:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
168:00kelvin8:00mary8:00hugo
178:00kelvin8:00mary8:00hugo
188:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
198:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
208:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
218:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
228:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
238:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
248:30apple8:30peter8:30gary8:30tom8:00mary8:00hugo
258:30apple8:30peter8:30gary8:30tom8:00mary8:00hugo
269:00gary8:30kelvin8:00mary8:00hugo
279:00gary8:30kelvin8:00mary8:00hugo
289:00apple9:00peter9:00gary9:00tom8:30kelvin8:00mary8:00hugo
299:00apple9:00peter9:00gary9:00tom8:30kelvin8:00mary8:00hugo
309:00apple9:00peter9:00gary9:00tom8:30kelvin8:00mary8:00hugo
319:00apple9:00peter9:00tom
329:00apple9:00peter9:00tom
3310:15gary9:45kelvin8:30mary8:30hugo
348:30mary8:30hugo
3510:15apple10:15peter10:15tom8:30mary8:30hugo
3611:30gary11:30kelvin8:30mary8:30hugo
378:30mary8:30hugo
3811:30apple11:30peter11:30tom8:30mary8:30hugo
3912:30gary12:30kelvin
4012:30kelvin
4112:30apple12:30peter12:30tom9:45mary9:45hugo
4213:15gary
4313:15gary13:15kelvin
4413:15apple13:15peter13:15gary13:15tom13:15kelvin11:00mary11:00hugo
4513:15apple13:15peter13:15gary13:15tom13:15kelvin11:00mary11:00hugo
4613:15apple13:15peter13:15gary13:15tom13:15kelvin
4713:15apple13:15peter13:15gary13:15tom13:15kelvin
4813:15apple13:15peter13:15gary13:15tom13:15kelvin11:30mary11:30hugo
4913:15apple13:15peter13:15gary13:15tom13:15kelvin
5013:15apple13:15peter13:15gary13:15tom13:15kelvin
5113:15apple13:15peter13:15gary13:15tom13:15kelvin12:30mary12:30hugo
5213:15apple13:15peter13:15gary13:15tom13:15kelvin12:30mary12:30hugo
5313:15apple13:15peter13:15gary13:15tom13:15kelvin12:30mary12:30hugo
5413:15apple13:15peter13:15tom13:15kelvin12:30mary12:30hugo
5513:15apple13:15peter13:15tom13:15kelvin
5615:30gary
5713:15mary13:15hugo
5815:30apple15:30peter15:30tom15:30kelvin13:15mary13:15hugo
5913:15mary13:15hugo
6013:15mary13:15hugo
6113:15mary13:15hugo
6213:15mary13:15hugo
6313:15mary13:15hugo
6413:15mary13:15hugo
6513:15mary13:15hugo
6613:15mary13:15hugo
6713:15mary13:15hugo
6813:15mary13:15hugo
6913:15mary13:15hugo
7013:15mary13:15hugo
7113:15mary13:15hugo
7213:15mary13:15hugo
7313:15mary13:15hugo
7413:15mary13:15hugo
7513:15mary13:15hugo
7613:15mary13:15hugo
7713:15mary13:15hugo
78
79
8015:30mary15:30hugo
81
82
week1




test.xlsx
ABCDEFGHIJKLMNOP
1
2
3
4
5
6
7
802/02/202502/03/202502/04/202502/05/202502/06/202502/07/202502/08/2025
9
10
week2
 
Try this:

VBA Code:
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:I" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 14)
  Sheets("week1").Range("B8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("B8:O" & Rows.Count).ClearContents
  
  aDate = a(1, 5):  aName = a(1, 6):  aTime = a(1, 8)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 5) Then
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 5)
    End If
    If aName <> a(i, 6) Or aTime <> a(i, 8) Then k = k + 2
    
    b(k, j) = a(i, 8)
    b(k, j + 1) = a(i, 6)
    aDate = a(i, 5): aName = a(i, 6): aTime = a(i, 8)
  Next
  
  Sheets("week2").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

😇
 
Upvote 0
Try this:

VBA Code:
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:I" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 14)
  Sheets("week1").Range("B8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("B8:O" & Rows.Count).ClearContents
 
  aDate = a(1, 5):  aName = a(1, 6):  aTime = a(1, 8)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 5) Then
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 5)
    End If
    If aName <> a(i, 6) Or aTime <> a(i, 8) Then k = k + 2
  
    b(k, j) = a(i, 8)
    b(k, j + 1) = a(i, 6)
    aDate = a(i, 5): aName = a(i, 6): aTime = a(i, 8)
  Next
 
  Sheets("week2").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

😇
thank you very much for your reply, DanteAmor

THIS IS REAL DOPE AND IMPECCABLE!!!!!

however:

1) the code can run exactly what i want but i found when i put this code to my workbook(obviously not only one employee per day)
it turns out sepearted every start time like below screenshot

test.xlsx
ABCDEFGHI
3Worker TypeLOBLocationPerson No.DateNameJOBStart TimeEnd Time
401/26/2025apple8:158:45
501/26/2025peter8:1517:45
601/26/2025apple8:1517:45
701/26/2025peter8:1517:45
801/26/2025apple8:1517:45
901/26/2025peter8:1517:45
1001/26/2025apple8:1517:45
1101/26/2025peter8:3018:00
1201/26/2025apple8:3018:00
1301/26/2025peter8:3018:00
1401/26/2025apple8:3018:00
1501/26/2025peter8:3018:00
1601/26/2025apple8:3018:00
list



test.xlsx
ABCDEFGHIJKLMNO
801/26/202501/27/202501/28/202501/29/202501/30/202501/31/202502/01/2025
98:15apple8:15apple8:15apple8:15apple8:00apple8:00apple8:00apple
10
11
128:15peter8:15peter8:15peter8:15peter8:00peter8:00peter8:00peter
13
14
158:15apple8:15apple8:15apple8:15apple8:00apple8:00apple8:00apple
16
17
188:15peter8:15peter8:15peter8:15peter8:00peter8:00peter8:00peter
19
20
218:15apple8:15apple8:15apple8:15apple8:00apple8:00apple8:00apple
22
23
248:15peter8:15peter8:15peter8:15peter45687.333peter8:00peter8:00peter
25
26
278:15apple45684.34apple8:15apple45686.344apple8:00apple8:00apple8:00apple
28
week1



2) my tech team swapped the 2 columns on col E and col F
col E - date > col F
col F - name > col E
can you show me how do i amend your code?

thank you very much for you guidance and support
 
Upvote 0
it turns out sepearted every start time
This is solved if before running the macro, you sort your data by Date, Name and start time.


2) my tech team swapped the 2 columns on col E and col F
col E - date > col F
col F - name > col E
can you show me how do i amend your code?

I marked the changes in blue:

Rich (BB code):
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:I" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 14)
  Sheets("week1").Range("B8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("B8:O" & Rows.Count).ClearContents
  
  aDate = a(1, 6):  aName = a(1, 5):  aTime = a(1, 8)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(i, 8) Then k = k + 2
    
    b(k, j) = a(i, 8)
    b(k, j + 1) = a(i, 5)         'name
    aDate = a(i, 6): aName = a(i, 5): aTime = a(i, 8)
  Next
  
  Sheets("week2").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

😇
 
Upvote 0
This is solved if before running the macro, you sort your data by Date, Name and start time.




I marked the changes in blue:

Rich (BB code):
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:I" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 14)
  Sheets("week1").Range("B8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("B8:O" & Rows.Count).ClearContents
 
  aDate = a(1, 6):  aName = a(1, 5):  aTime = a(1, 8)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(i, 8) Then k = k + 2
   
    b(k, j) = a(i, 8)
    b(k, j + 1) = a(i, 5)         'name
    aDate = a(i, 6): aName = a(i, 5): aTime = a(i, 8)
  Next
 
  Sheets("week2").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

😇
thank you very much for your reply, DanteAmo

1) same outcome after sort name, date, start time. i was thinking, is it the name comes with "initial + full name" casued the issue?
ie. Summie, Chan Chi Ming

2) like your changes in blue, if the strat time change to anoter column(example J)
am i simply change like this in red?
Rich (BB code):
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:I" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 14)
  Sheets("week1").Range("B8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("B8:O" & Rows.Count).ClearContents
 
  aDate = a(1, 6):  aName = a(1, 5):  aTime = a(1, 10)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(1, 10) Then k = k + 2
   
    b(k, j) = a(1, 10)
    b(k, j + 1) = a(i, 5)         'name
    aDate = a(i, 6): aName = a(i, 5): aTime = a(1, 10)
  Next
 
  Sheets("week2").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

thank you very much for you guidance and support
 
Upvote 0
This is solved if before running the macro, you sort your data by Date, Name and start time.




I marked the changes in blue:

Rich (BB code):
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:I" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 14)
  Sheets("week1").Range("B8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("B8:O" & Rows.Count).ClearContents
 
  aDate = a(1, 6):  aName = a(1, 5):  aTime = a(1, 8)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(i, 8) Then k = k + 2
   
    b(k, j) = a(i, 8)
    b(k, j + 1) = a(i, 5)         'name
    aDate = a(i, 6): aName = a(i, 5): aTime = a(i, 8)
  Next
 
  Sheets("week2").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

😇
Hi DanteAmor

i amened the code like below, but still can not fix the outcome even sort before run the code by your suggestion
example. Summie, Chan Chi Ming

VBA Code:
Sub ExportByDate()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long

  a = Sheets("list").Range("A4:k" & Sheets("list").Range("E" & Rows.Count).End(3).Row).Value
  ReDim C(1 To UBound(a, 1), 1 To 14)
  Sheets("week2").Range("c8:p" & Rows.Count).ClearContents
  
  aDate = a(1, 6):  aName = a(1, 5):  aTime = a(1, 10)
  C(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("C8").Resize(UBound(C, 1), UBound(C, 2)).Value = C
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      C(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(i, 10) Then k = k + 2
    
    C(k, j) = a(i, 10)
    C(k, j + 1) = a(i, 5)         'name
    aDate = a(i, 6): aName = a(i, 5): aTime = a(i, 10)
  Next
  
  Sheets("week2").Range("C8").Resize(UBound(C, 1), UBound(C, 2)).Value = C
End Sub
 
Upvote 0
1738537659344.png

I don't understand why you set those values.
You can put the minisheets here so that I can see how the data really is and what the result you want is.

:unsure:
 
Upvote 0
View attachment 121920
I don't understand why you set those values.
You can put the minisheets here so that I can see how the data really is and what the result you want is.

:unsure:
Hi DanteAmor
thank you very much for your reply

please allow me to start it over and please forgive my presentation as my english is not mother tone

#1) my workbook has 3 sheets so far, "list", "week1", and "week2", this is the lastest version updated by my tech team
#2) i need a macro to run the first 7days into "week1" , and next 7days into "week2" from "list"
#3) i need 2 empty rows to sepearte each start time for easy reference
#4) just to pay safe, i want to start at column C instead of column B on "week1" and "week2"
#5) the name format in column E is "initial, full name" (example: Summie, Chan Chi Ming), but i can't return like you, even i use
Excel Formula:
=LEFT(A2, FIND(",", A2)-1)
to extract and run only by initial, i got the same result
#6) your coding in #2 was brillant, but i get stucks when i change those names back to my employee's name format, even though you said sort by "name, date, and start time" first, it still breaks 2 rows for every start time

thank you very much for your guidance again and again

Store List (0209-0222).xls
ABCDEFGHIJKL
1HKDL Schedule List
2671078 1/22/2025 5:57:46 PM
3Worker TypeLOBLocationPerson No.NameDateJOBWBSWorkruleStart TimeEnd Time
4Ally02/09/20258:1517:45
5Carol02/09/20258:1517:45
6Catherine02/09/20258:1517:45
7Kelvin02/09/20258:1517:45
8Nicole02/09/20258:1517:45
9Summie02/09/20258:158:45
10Yuki02/09/20258:1517:45
11Amy02/09/20258:3018:00
12Andy02/09/20258:3018:00
13Ken02/09/20258:3018:00
14Mavis02/09/20258:3018:00
15Nickey02/09/20258:3018:00
16Selly02/09/20258:3018:00
17Sharon02/09/20258:3018:00
18Sunnie02/09/20258:3018:00
19Tracy02/09/20258:3018:00
20Emily02/09/20259:0018:30
21Jessica02/09/20259:0018:30
22Kelly02/09/20259:0018:30
23Dennis02/09/20259:1518:45
24Holiday02/09/20259:1518:45
25Summie02/09/202510:1515:30
26Yumi02/09/202512:3022:00
27Zarlish02/09/202512:3022:00
28Bowie02/09/202513:1522:45
29Chocolate02/09/202513:1522:45
30Derrick02/09/202513:1522:45
31Icy02/09/202513:1522:45
32Jesse02/09/202513:1522:45
33Kali02/09/202513:1522:45
34Kin02/09/202513:1522:45
list
 
Upvote 0
#5) the name format in column E is "initial, full name" (example: Summie, Chan Chi Ming), but i can't return like you, even i use
to extract and run only by initial, i got the same result
I don't understand that part. But I'll try.

So first run this version:
VBA Code:
Sub ExportByDate_v1()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long, lr As Long

  'sort list by date, time, name
  With Sheets("list")
    lr = .Range("E" & Rows.Count).End(3).Row
    With .Range("A4:K" & lr)
      .Sort .Range("F3"), xlAscending, .Range("J3"), , xlAscending, .Range("E3"), xlAscending, xlYes
      a = .Value
    End With
  End With
  ReDim b(1 To UBound(a, 1) * 3, 1 To 14)
  Sheets("week1").Range("C8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("C8:O" & Rows.Count).ClearContents
  
  aName = a(1, 5): aDate = a(1, 6): aTime = a(1, 10)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(i, 10) Then k = k + 2
    
    b(k, j) = a(i, 10)            'start time
    b(k, j + 1) = a(i, 5)         'name
    aName = a(i, 5): aDate = a(i, 6): aTime = a(i, 10)
  Next
  
  If n < 8 Then
    Sheets("week1").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Else
    Sheets("week2").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End If
End Sub

Version with initial name:
VBA Code:
Sub ExportByDate_v2()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant, auxName As String
  Dim i As Long, j As Long, k As Long, n As Long, lr As Long

  'sort list by date, time, name
  With Sheets("list")
    lr = .Range("E" & Rows.Count).End(3).Row
    With .Range("A4:K" & lr)
      .Sort .Range("F3"), xlAscending, .Range("J3"), , xlAscending, .Range("E3"), xlAscending, xlYes
      a = .Value
    End With
  End With
  ReDim b(1 To UBound(a, 1) * 3, 1 To 14)
  Sheets("week1").Range("C8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("C8:O" & Rows.Count).ClearContents
  
  If InStr(1, a(1, 5), ",") > 0 Then aName = Split(a(1, 5), ",")(0) Else aName = a(1, 5)
  aDate = a(1, 6): aTime = a(1, 10)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    
    If InStr(1, a(i, 5), ",") > 0 Then auxName = Split(a(i, 5), ",")(0) Else auxName = a(i, 5)
    If aName <> auxName Or aTime <> a(i, 10) Then k = k + 2
    
    b(k, j) = a(i, 10)            'start time
    b(k, j + 1) = auxName         'name
    aName = auxName: aDate = a(i, 6): aTime = a(i, 10)
  Next
  
  If n < 8 Then
    Sheets("week1").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Else
    Sheets("week2").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End If
End Sub

🫡
 
Upvote 0
I don't understand that part. But I'll try.

So first run this version:
VBA Code:
Sub ExportByDate_v1()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant
  Dim i As Long, j As Long, k As Long, n As Long, lr As Long

  'sort list by date, time, name
  With Sheets("list")
    lr = .Range("E" & Rows.Count).End(3).Row
    With .Range("A4:K" & lr)
      .Sort .Range("F3"), xlAscending, .Range("J3"), , xlAscending, .Range("E3"), xlAscending, xlYes
      a = .Value
    End With
  End With
  ReDim b(1 To UBound(a, 1) * 3, 1 To 14)
  Sheets("week1").Range("C8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("C8:O" & Rows.Count).ClearContents
 
  aName = a(1, 5): aDate = a(1, 6): aTime = a(1, 10)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
    If aName <> a(i, 5) Or aTime <> a(i, 10) Then k = k + 2
   
    b(k, j) = a(i, 10)            'start time
    b(k, j + 1) = a(i, 5)         'name
    aName = a(i, 5): aDate = a(i, 6): aTime = a(i, 10)
  Next
 
  If n < 8 Then
    Sheets("week1").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Else
    Sheets("week2").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End If
End Sub

Version with initial name:
VBA Code:
Sub ExportByDate_v2()
  Dim a As Variant, b As Variant, aDate As Variant, aName As Variant, aTime As Variant, auxName As String
  Dim i As Long, j As Long, k As Long, n As Long, lr As Long

  'sort list by date, time, name
  With Sheets("list")
    lr = .Range("E" & Rows.Count).End(3).Row
    With .Range("A4:K" & lr)
      .Sort .Range("F3"), xlAscending, .Range("J3"), , xlAscending, .Range("E3"), xlAscending, xlYes
      a = .Value
    End With
  End With
  ReDim b(1 To UBound(a, 1) * 3, 1 To 14)
  Sheets("week1").Range("C8:O" & Rows.Count).ClearContents
  Sheets("week2").Range("C8:O" & Rows.Count).ClearContents
 
  If InStr(1, a(1, 5), ",") > 0 Then aName = Split(a(1, 5), ",")(0) Else aName = a(1, 5)
  aDate = a(1, 6): aTime = a(1, 10)
  b(1, 2) = aDate
  k = 1:  j = 1:  n = 1
  For i = 1 To UBound(a)
    k = k + 1
    If aDate <> a(i, 6) Then      'date
      k = 0
      n = n + 1
      If n = 8 Then
        j = 1
        Sheets("week1").Range("B8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
        ReDim b(1 To UBound(a, 1), 1 To 14)
      Else
        j = j + 2
      End If
      b(1, j + 1) = a(i, 6)       'date
    End If
   
    If InStr(1, a(i, 5), ",") > 0 Then auxName = Split(a(i, 5), ",")(0) Else auxName = a(i, 5)
    If aName <> auxName Or aTime <> a(i, 10) Then k = k + 2
   
    b(k, j) = a(i, 10)            'start time
    b(k, j + 1) = auxName         'name
    aName = auxName: aDate = a(i, 6): aTime = a(i, 10)
  Next
 
  If n < 8 Then
    Sheets("week1").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Else
    Sheets("week2").Range("C8").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End If
End Sub

🫡
after the first code, below is part of the result on "week1"
-the code start at column B, i need it at column C
-every 2 empty rows for each employee but not each start time, i dont need empty rows between every employee
MSS Store List (0209-0222).xls
ABCDEFG
1
2
3
4
5
6
7
89/2/202510/2/202511/2/2025
945697.34Summie, Hx Wxx Txx45698.34Carol, Kxx Wxx Hxx45699.34Derrick, Cxx Kxx Cxx
10
11
1245697.34Ally, Cxx HxxMxx45698.34James, Fxx Cxx Kxx45699.34Lina, Bxx Pxx
13
14
1545697.34Carol, Kxx Wxx Hxx45698.34Jennifer, Txx Kxx Lxx45699.34Ming, Wxx Cxx Mxx
week1



after the first code, below is part of the result on "week2"
-every 2 empty rows for each employee but not each start time, i dont need empty rows between every employee
MSS Store List (0209-0222).xls
ABCDEFGH
1
2
3
4
5
6
7
816/2/202517/2/202518/2/2025
945704.34Howard, Cxx Hxx Cxx45705.34Andy, Cxx Yxx Hxx45706.34Andy, Cxx Yxx Hxx
10
11
1245704.34Kin, Nxx Yxx Lxx45705.34Chloe, Cxx Cxx Cxx45706.34Carol, Kxx Wxx Hxx
13
14
1545704.34Margaret, Txx Yxx Cxx45705.34Hiko, Lxx Pxx Pxx45706.34Derrick, Cxx Kxx Cxx
week2



after the second code, below is part of the result on "week1", same as "week2"
-the first code with the "full name" is good, can i keep this?
MSS Store List (0209-0222).xls
ABCDEFGHI
1
2
3
4
5
6
7
89/2/202510/2/202511/2/202512/2/2025
945697.34Summie45698.34Carol45699.34Derrick45700.34Ally
10
11
1245697.34Ally45698.34James45699.34Lina45700.34Chocolate
13
14
1545697.34Carol45698.34Jennifer45699.34Ming45700.34Ivy
16
17
1845697.34Catherine45698.34Lina45699.34Rainbow45700.34Thetis
19
20
2145697.34Kelvin45698.34Nickey45699.34Sophia45700.34Toru
22
23
2445697.34Nicole45698.34Shirley45699.34Walter45700.34Wynter
25
26
2745697.34Yuki45698.34Tracy45699.34Winnie45700.34Yuki
week1



-can i return the result like this?
test.xlsx
ABCDEFGHIJKLMNO
1
2
3
4
5
6
7
801/26/202501/27/202501/28/202501/29/202501/30/202501/31/202502/01/2025
98:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
108:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
118:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
128:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
138:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
148:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
158:15apple8:15peter8:15gary8:15tom8:00kelvin8:00mary8:00hugo
168:00kelvin8:00mary8:00hugo
178:00kelvin8:00mary8:00hugo
188:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
198:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
208:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
218:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
228:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
238:30apple8:30peter8:30gary8:30tom8:00kelvin8:00mary8:00hugo
248:30apple8:30peter8:30gary8:30tom8:00mary8:00hugo
258:30apple8:30peter8:30gary8:30tom8:00mary8:00hugo
269:00gary8:30kelvin8:00mary8:00hugo
279:00gary8:30kelvin8:00mary8:00hugo
289:00apple9:00peter9:00gary9:00tom8:30kelvin8:00mary8:00hugo
299:00apple9:00peter9:00gary9:00tom8:30kelvin8:00mary8:00hugo
309:00apple9:00peter9:00gary9:00tom8:30kelvin8:00mary8:00hugo
319:00apple9:00peter9:00tom
329:00apple9:00peter9:00tom
3310:15gary9:45kelvin8:30mary8:30hugo
348:30mary8:30hugo
3510:15apple10:15peter10:15tom8:30mary8:30hugo
week1


🙇‍♀️🙇‍♀️🙇‍♀️
 
Upvote 0

Forum statistics

Threads
1,226,848
Messages
6,193,315
Members
453,790
Latest member
yassinosnoo1

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