Add rows and customized the date

sanj_edu

New Member
Joined
Dec 12, 2017
Messages
11
The actual data is below:

[TABLE="width: 918"]
<tbody>[TR]
[TD]S. No[/TD]
[TD]First Name[/TD]
[TD]Last Name[/TD]
[TD]Gen ID[/TD]
[TD]Band[/TD]
[TD]Start Date[/TD]
[TD]End Date[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]20-01-2017[/TD]
[TD]14-12-2017[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Abdul[/TD]
[TD]Karenina[/TD]
[TD]33372[/TD]
[TD]S2[/TD]
[TD]25-10-2017[/TD]
[TD]28-11-2017[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Abe[/TD]
[TD]Sarandon[/TD]
[TD]33373[/TD]
[TD]S3[/TD]
[TD]18-05-2017[/TD]
[TD]20-07-2017[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Abraham[/TD]
[TD]D'Arby[/TD]
[TD]33375[/TD]
[TD]S5[/TD]
[TD]15-03-2017[/TD]
[TD]20-05-2017[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Abram[/TD]
[TD]Palyuvchenko[/TD]
[TD]33376[/TD]
[TD]S6[/TD]
[TD]08-01-2017[/TD]
[TD]09-01-2017[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]sarftyu[/TD]
[TD]Sarandon[/TD]
[TD]33373[/TD]
[TD]S3[/TD]
[TD]21-05-2017[/TD]
[TD]20-07-2017[/TD]
[/TR]
</tbody><colgroup><col><col span="6"></colgroup>[/TABLE]


the End Result should be like:


[TABLE="width: 928"]
<tbody>[TR]
[TD]S. No[/TD]
[TD]First Name[/TD]
[TD]Last Name[/TD]
[TD]Gen ID[/TD]
[TD]Band[/TD]
[TD]Start Date[/TD]
[TD]End Date[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]20-01-2017[/TD]
[TD]31-01-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-02-2017[/TD]
[TD]28-02-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-03-2017[/TD]
[TD]31-03-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-04-2017[/TD]
[TD]30-04-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-05-2017[/TD]
[TD]31-05-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-06-2017[/TD]
[TD]30-06-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-07-2017[/TD]
[TD]31-07-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-08-2017[/TD]
[TD]31-08-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-09-2017[/TD]
[TD]30-09-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-10-2017[/TD]
[TD]31-10-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-11-2017[/TD]
[TD]30-11-2017[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Aaron[/TD]
[TD]Small[/TD]
[TD]33371[/TD]
[TD]S1[/TD]
[TD]01-12-2017[/TD]
[TD]14-12-2017[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Abdul[/TD]
[TD]Karenina[/TD]
[TD]33372[/TD]
[TD]S2[/TD]
[TD]25-10-2017[/TD]
[TD]28-11-2017[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Abdul[/TD]
[TD]Karenina[/TD]
[TD]33372[/TD]
[TD]S2[/TD]
[TD]25-10-2017[/TD]
[TD]31-10-2017[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Abdul[/TD]
[TD]Karenina[/TD]
[TD]33372[/TD]
[TD]S2[/TD]
[TD]01-11-2017[/TD]
[TD]28-11-2017[/TD]
[/TR]
</tbody><colgroup><col><col span="6"></colgroup>[/TABLE]
 

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".
Welcome to the Board!

Assuming that your data is in columns A-G, your header is in row 1, and your data begins on row 2, this VBA code should do what you want:
Code:
Sub MyInsertMacro()

    Dim myRow As Long
    Dim stDate As Date
    Dim endDate As Date
    Dim eomDate As Date
        
    Application.ScreenUpdating = False

'   Enter first row of data    
    myRow = 2

'   Loop until column A is blank
    Do Until Cells(myRow, "A") = ""
'       Get dates from row
        stDate = Cells(myRow, "F")
        endDate = Cells(myRow, "G")
'       Calculate end of month date from initial start date
        eomDate = DateSerial(Year(stDate), Month(stDate) + 1, 0)
'       Check to see if end date is greater than end of month date
        If endDate > eomDate Then
'           Insert new row
            Rows(myRow + 1).Insert
'           Copy first five columns
            Range(Cells(myRow, "A"), Cells(myRow, "E")).Copy Cells(myRow + 1, "A")
'           Update end date on first record
            Cells(myRow, "G") = eomDate
'           Update dates on new row
            Cells(myRow + 1, "F") = eomDate + 1
            Cells(myRow + 1, "G") = endDate
        End If
'       Increment row counter
        myRow = myRow + 1
    Loop

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hi,

Joe4, It works perfectly fine. You are really the Best.

Thanks a lot for such a quick response!!.


Best Forum to discuss!!!



Welcome to the Board!

Assuming that your data is in columns A-G, your header is in row 1, and your data begins on row 2, this VBA code should do what you want:
Code:
Sub MyInsertMacro()

    Dim myRow As Long
    Dim stDate As Date
    Dim endDate As Date
    Dim eomDate As Date
        
    Application.ScreenUpdating = False

'   Enter first row of data    
    myRow = 2

'   Loop until column A is blank
    Do Until Cells(myRow, "A") = ""
'       Get dates from row
        stDate = Cells(myRow, "F")
        endDate = Cells(myRow, "G")
'       Calculate end of month date from initial start date
        eomDate = DateSerial(Year(stDate), Month(stDate) + 1, 0)
'       Check to see if end date is greater than end of month date
        If endDate > eomDate Then
'           Insert new row
            Rows(myRow + 1).Insert
'           Copy first five columns
            Range(Cells(myRow, "A"), Cells(myRow, "E")).Copy Cells(myRow + 1, "A")
'           Update end date on first record
            Cells(myRow, "G") = eomDate
'           Update dates on new row
            Cells(myRow + 1, "F") = eomDate + 1
            Cells(myRow + 1, "G") = endDate
        End If
'       Increment row counter
        myRow = myRow + 1
    Loop

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
You are welcome!

I tried to add lots of comments to the code to explain what each step is doing.
Hopefully, it all makes sense.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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