Add rows and customized the date

sanj_edu

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

S. NoFirst NameLast NameGen IDBandStart DateEnd Date
1AaronSmall33371S120-01-201714-12-2017
2AbdulKarenina33372S225-10-201728-11-2017
3AbeSarandon33373S318-05-201720-07-2017
4AbrahamD'Arby33375S515-03-201720-05-2017
5AbramPalyuvchenko33376S608-01-201709-01-2017
6sarftyuSarandon33373S321-05-201720-07-2017

<tbody>
</tbody><colgroup><col><col span="6"></colgroup>


the End Result should be like:


S. NoFirst NameLast NameGen IDBandStart DateEnd Date
1AaronSmall33371S120-01-201731-01-2017
1AaronSmall33371S101-02-201728-02-2017
1AaronSmall33371S101-03-201731-03-2017
1AaronSmall33371S101-04-201730-04-2017
1AaronSmall33371S101-05-201731-05-2017
1AaronSmall33371S101-06-201730-06-2017
1AaronSmall33371S101-07-201731-07-2017
1AaronSmall33371S101-08-201731-08-2017
1AaronSmall33371S101-09-201730-09-2017
1AaronSmall33371S101-10-201731-10-2017
1AaronSmall33371S101-11-201730-11-2017
1AaronSmall33371S101-12-201714-12-2017
2AbdulKarenina33372S225-10-201728-11-2017
2AbdulKarenina33372S225-10-201731-10-2017
2AbdulKarenina33372S201-11-201728-11-2017

<tbody>
</tbody><colgroup><col><col span="6"></colgroup>
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
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
 

sanj_edu

New Member
Joined
Dec 12, 2017
Messages
11
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,327
Members
414,053
Latest member
Dual Showman

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
Top