Duplicate entries based on start and end date

rubbis_with_excel

New Member
Joined
Jan 19, 2004
Messages
28
Hi,
I have some data with start and end dates, and I need to build some VBA to duplicate lines so there are month-specific entries. Hopefully this example *might* explain it :)

If the raw data looks like this:
<b>Name___Start Date___End Date</b>
Person1__01/01/2019___31/03/2019
Person2__10/10/2019___11/11/2019

I want to be able to convert it to:
<b>Name___Start Date___End Date</b>
Person1__01/01/2019___31/01/2019
Person1__01/02/2019___28/02/2019
Person1__01/03/2019___31/03/2019
Person2__10/10/2019___31/10/2019
Person2__01/11/2019___11/11/2019

So that each month is represented on a different row.

I've been racking my brains for hours and I just can't get it to work! :(

If anyone has any ideas, I would be very grateful.

Thanks
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,494
Office Version
365
Platform
Windows
Try this (I added lots of documentation so hopefully you can see how it works):
Code:
Sub MyInsertRows()

    Dim r As Long
    Dim dteStart As Date
    Dim dteEnd As Date
    Dim dteEOM As Date
    Dim nm As String

    Application.ScreenUpdating = False
    
'   Specify first row data appears on
    r = 2
    
'   Loop through all records
    Do Until Cells(r, "B") = ""
'       Get current values
        nm = Cells(r, "A")
        dteStart = Cells(r, "B")
        dteEnd = Cells(r, "C")
'       Calculate end of month date from start date
        dteEOM = DateSerial(Year(dteStart), Month(dteStart) + 1, 0)
'       Check to see if current end date greater than end of month date
        If dteEnd > dteEOM Then
'           Insert record and update dates
            Rows(r + 1).Insert
'           Update End date on current record
            Cells(r, "C") = dteEOM
'           Add values for new record
            Cells(r + 1, "A") = nm
            Cells(r + 1, "B") = dteEOM + 1
            Cells(r + 1, "C") = dteEnd
        End If
'       Increment row counter
        r = r + 1
    Loop
    
    Application.ScreenUpdating = True
    
End Sub
Note that I am assuming your data is in columns A, B, and C, and the header row is row 1 and the data starts on row 2.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,494
Office Version
365
Platform
Windows
You are welcome.
Glad I was able to help!
:)
 

Forum statistics

Threads
1,082,360
Messages
5,364,935
Members
400,815
Latest member
Joaquin Phoenix

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top