Create New Rows based on Start & End Date

harishs

Board Regular
Joined
Jul 3, 2016
Messages
50
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Experts,

I am looking for a macro code to create rows per employee based on the start & end date. Each employee will have LOP dates with From & To date, I need to create each row per employee for the dates available between dates.

Attached screenshot for reference.

Regards,
Harish S
 

Attachments

  • Create Rows.JPG
    Create Rows.JPG
    111.1 KB · Views: 26

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If your data is like this on sheet 1.
Dante Amor
ABCDEF
1IDEMPLOYEEATTENDANCEFROM DATETO DATESTATUS
2Test 1Name 1LOP03/04/202008/04/2020
3Test 2Name 2LOP25/01/202002/02/2020
4Test 2Name 2LOPR06/02/202023/02/2020
Sheet1


The results will be like this on sheet2:
Dante Amor
ABCDEF
1IDEMPLOYEEATTENDANCEFROM DATETO DATESTATUS
2Test 1Name 1LOP03/04/202003/04/2020
3Test 1Name 1LOP04/04/202004/04/2020
4Test 1Name 1LOP05/04/202005/04/2020
5Test 1Name 1LOP06/04/202006/04/2020
6Test 1Name 1LOP07/04/202007/04/2020
7Test 1Name 1LOP08/04/202008/04/2020
8Test 2Name 2LOP25/01/202025/01/2020
9Test 2Name 2LOP26/01/202026/01/2020
10Test 2Name 2LOP27/01/202027/01/2020
11Test 2Name 2LOP28/01/202028/01/2020
12Test 2Name 2LOP29/01/202029/01/2020
13Test 2Name 2LOP30/01/202030/01/2020
14Test 2Name 2LOP31/01/202031/01/2020
15Test 2Name 2LOP01/02/202001/02/2020
16Test 2Name 2LOP02/02/202002/02/2020
Sheet2


Try this:
VBA Code:
Sub Create_New_Rows()
  Dim a As Variant, b As Variant, nMax As Double
  Dim i As Long, j As Long, k As Long, lr As Long
  '
  With Sheets("Sheet1")
    lr = .Range("A" & Rows.Count).End(3).Row
    a = .Range("A2:F" & lr).Value2
    nMax = Evaluate("=MAX('" & .Name & "'!E2:E" & lr & "-'" & .Name & "'!D2:D" & lr & ")")
    ReDim b(1 To UBound(a) * nMax, 1 To 6)
  End With
  '
  For i = 1 To UBound(a, 1)
    For j = a(i, 4) To a(i, 5)
      k = k + 1
      b(k, 1) = a(i, 1)
      b(k, 2) = a(i, 2)
      b(k, 3) = a(i, 3)
      b(k, 4) = j
      b(k, 5) = j
      b(k, 6) = a(i, 6)
    Next
  Next
  '
  With Sheets("Sheet2")
    .Rows("2:" & Rows.Count).ClearContents
    .Range("A2").Resize(k, 6).Value = b
  End With
End Sub
 
Upvote 0
If your data is not thousands, here is another macro with a simpler code.

VBA Code:
Sub test1()
  Dim c As Range, n As Long, lr As Long
  For Each c In Range("A2", Range("A" & Rows.Count).End(3))
    n = c.Offset(, 4) - c.Offset(, 3) + 1
    lr = Range("H" & Rows.Count).End(3).Row + 1
    Range("H" & lr).Resize(n, 6).Value = Array(c, c.Offset(, 1), c.Offset(, 2), c.Offset(, 3), c.Offset(, 3), c.Offset(, 5))
    Range("K" & lr).Resize(n, 2).DataSeries xlColumns, xlChronological, xlDay, 1, , False
  Next
End Sub

Results in G2 onwards:
Dante Amor
ABCDEFGHIJKLM
1IDEMPLOYEEATTENDANCEFROM DATETO DATESTATUSIDEMPLOYEEATTENDANCEFROM DATETO DATESTATUS
2Test 1Name 1LOP03/04/202008/04/2020st1Test 1Name 1LOP03/04/202003/04/2020st1
3Test 2Name 2LOP25/01/202002/02/2020st2Test 1Name 1LOP04/04/202004/04/2020st1
4Test 2Name 2LOPR06/02/202023/02/2020st3Test 1Name 1LOP05/04/202005/04/2020st1
5Test 1Name 1LOP06/04/202006/04/2020st1
6Test 1Name 1LOP07/04/202007/04/2020st1
7Test 1Name 1LOP08/04/202008/04/2020st1
8Test 2Name 2LOP25/01/202025/01/2020st2
9Test 2Name 2LOP26/01/202026/01/2020st2
10Test 2Name 2LOP27/01/202027/01/2020st2
11Test 2Name 2LOP28/01/202028/01/2020st2
12Test 2Name 2LOP29/01/202029/01/2020st2
13Test 2Name 2LOP30/01/202030/01/2020st2
14Test 2Name 2LOP31/01/202031/01/2020st2
15Test 2Name 2LOP01/02/202001/02/2020st2
16Test 2Name 2LOP02/02/202002/02/2020st2
Sheet1
 
Upvote 0
Hi Dante,

Thank You So Much, both your codes work like a charm.

Regards,
Harish S
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

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