Code to Insert Rows below data with Specified Dates Based on Data in Another Column

Ebonii

New Member
Joined
Oct 4, 2013
Messages
3
Hi, I'm new so be gentle. This is a bit complex for me so I'm completely lost.


Below is an example of what I receive from our system, except imagine it about 200 columns across and up to 8,000 rows down. So manual manipulation is not only horrid but a risk of error.

Column's 'A' and 'B' are basic data. Column 'C' is the start date for the hours listed in Column's D-F. Columns D-F represent months that will go in order beginning with the date in column 'C'. So for instance in row 2, the start date is Dec-13, thus in row Month 1 is Dec-13, Month 2 would be Jan-14, and Month 3 would be Feb-2014. The logic continues for each row.


[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD][/TD]
[TD]A
[/TD]
[TD]B
[/TD]
[TD]C
[/TD]
[TD]D
[/TD]
[TD]E
[/TD]
[TD]F
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ID
[/TD]
[TD]Group
[/TD]
[TD]Start Date
[/TD]
[TD]Month 1
[/TD]
[TD]Month 2
[/TD]
[TD]Month 3
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Dec-13
[/TD]
[TD]46
[/TD]
[TD]5
[/TD]
[TD]12
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Jan-14
[/TD]
[TD]16
[/TD]
[TD]25
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Sep-13
[/TD]
[TD]3
[/TD]
[TD]3
[/TD]
[TD]6
[/TD]
[/TR]
</TBODY>[/TABLE]


Below is what I need to get to with VBA code. (Ignore the font colors, they are just there for emphasis)

I need the code to go to another specified sheet, (let's assume the headers below are already there) and populate the data as you see it below.

  1. I need each ID to have a row for the amount of months there are. (In this case 3 months, in my true case usually about 200 months.)
  2. The ID is duplicated on each row
  3. The corresponding group (column V is duplicated on each row
  4. Column 'C' in the "first" row of each ID on the destination sheet (reference red rows) should have the corresponding start date as column 'C' on the originating sheet. (i.e. cell C2 below matches 'C2' on start sheet, cell 'C5' matches 'C3' and so on)
  5. The dates below each of the "first" (red rows) in column 'C' should be based on the date in the cell above it.
  6. The data in column 'D' should be pulled from columns D-F of each ID on the starting sheet in order of the columns. Basically what happens here it sees that the start date for the info on the originating sheet, populates that in the starting row of each ID, then populates the next month below it for as many months as there are on the originating sheet.
  7. I would need this to continue to loop for as much data is on the originating sheet.


I hope I've explained this well. I cannot stress enough that the data will always come to me in that above table originating format and the columns and rows go on forever. So I'm dealing with massive amounts of data. I need this to be automated so as to minimize error and stop me from weeping softly in the corner whenver it's time to begin this task. Any help you can provide is appreciated. If you need more data or an attachment just let me know.

[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD][/TD]
[TD]A
[/TD]
[TD]B
[/TD]
[TD]C
[/TD]
[TD]D
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ID
[/TD]
[TD]Group
[/TD]
[TD]Month
[/TD]
[TD]Hours
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Dec-13
[/TD]
[TD]46
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Jan-14
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Feb-14
[/TD]
[TD]12
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Jan-14
[/TD]
[TD]16
[/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Feb-14
[/TD]
[TD]25
[/TD]
[/TR]
[TR]
[TD]7
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Mar-14
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]8
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Sep-13
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Oct-13
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]10
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Nov-13
[/TD]
[TD]6
[/TD]
[/TR]
</TBODY>[/TABLE]
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The attached code should fit your needs. It assumes the Source data is in Sheet 1 and the Destination data is posted to sheet 2

Code:
Option Explicit


Sub Process()
    Dim Ws As Worksheet
    Dim DstWs As Worksheet
    
    Dim SrcRowNo As Long
    Dim SrcColno As Long
    Dim DstRowNo As Long
    
    Set Ws = ThisWorkbook.Worksheets(1)
    Set DstWs = ThisWorkbook.Worksheets(2)
    
    DstRowNo = 2
    Application.ScreenUpdating = False
    
    For SrcRowNo = 2 To Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row
        Ws.Range(Ws.Cells(SrcRowNo, "A"), Ws.Cells(SrcRowNo, "C")).Copy
        For SrcColno = 4 To Ws.Cells(SrcRowNo, Ws.Columns.Count).End(xlToLeft).Column
            DstWs.Rows(DstRowNo).PasteSpecial
            DstWs.Cells(DstRowNo, "D") = Ws.Cells(SrcRowNo, SrcColno)
            DstRowNo = DstRowNo + 1
        Next SrcColno
        
        DoEvents
    Next SrcRowNo
    
    Application.ScreenUpdating = True
    
    MsgBox "Complete", vbInformation
End Sub
 
Upvote 0
First and foremost, you're amazing. I was losing my mind trying to get to this. So thank you for the quick response.

The code is about 95% where I need it to be. For some reason on the destination sheet in the Month column (C), dates are duplicating as opposed to populating that first month for each starting row from the originating sheet, then filling in the next calendar months in order dependent on the number of columns.


Basically what I'm getting when I run it is:

[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD][/TD]
[TD]A
[/TD]
[TD]B
[/TD]
[TD]C
[/TD]
[TD]D
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]ID
[/TD]
[TD]Group
[/TD]
[TD]Month
[/TD]
[TD]Hours
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Dec-13
[/TD]
[TD]46
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Dec-13
[/TD]
[TD]5
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]Number 1
[/TD]
[TD]A.02
[/TD]
[TD]Dec-13
[/TD]
[TD]12
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Jan-13
[/TD]
[TD]16
[/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Jan-13
[/TD]
[TD]25
[/TD]
[/TR]
[TR]
[TD]7
[/TD]
[TD]Number 2
[/TD]
[TD]A.04
[/TD]
[TD]Jan-13
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]8
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Sep-13
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Sep-13
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]10
[/TD]
[TD]Number 3
[/TD]
[TD]A.01
[/TD]
[TD]Sep-13
[/TD]
[TD]6
[/TD]
[/TR]
</TBODY>[/TABLE]


The only thing I need changed is column C. I need it to see that Start Date on the Destination sheet. Populate it in the first row of the unique ID (As your wonderful code currently does) but then in the following rows I need it to look at that start date and populate the next calendar month.

So referencing column C only below. In the destination sheet, column C should result in the month dates below. (The notes to the side are just indicators of what should be happening on that row in column 'C')

[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]C
[/TD]
[TD]Not part of needed code, just notes
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]Month
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]Dec-13
[/TD]
[TD]Start Date from Originating
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Jan-14
[/TD]
[TD]need code to populate next month based on month above
[/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]Feb-14
[/TD]
[TD]need code to populate next month based on month above
[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]Jan-13
[/TD]
[TD]Start Date from Originating
[/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD]Feb-13
[/TD]
[TD]need code to populate next month based on month above
[/TD]
[/TR]
[TR]
[TD]7
[/TD]
[TD]Mar-13
[/TD]
[TD]need code to populate next month based on month above
[/TD]
[/TR]
[TR]
[TD]8
[/TD]
[TD]Sep-13
[/TD]
[TD]Start Date from Originating
[/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD]Oct-13
[/TD]
[TD]need code to populate next month based on month above
[/TD]
[/TR]
[TR]
[TD]10
[/TD]
[TD]Nov-13
[/TD]
[TD]need code to populate next month based on month above
[/TD]
[/TR]
</TBODY>[/TABLE]


Again, I cannot stress enough, that EVERYTHING else in your code does every miracle thing I was failing to do last night. So if you know how to tweak the code to address this date issue, I'd be so grateful. This code has been troubling me for a week now.

Thank you in advance for your help.
 
Upvote 0
No problem, He is the modification that increments the date to your specs.

Code:
Option Explicit


Sub Process()
    Dim Ws As Worksheet
    Dim DstWs As Worksheet
    
    Dim SrcRowNo As Long
    Dim SrcColno As Long
    Dim DstRowNo As Long
    
    Set Ws = ThisWorkbook.Worksheets(1)
    Set DstWs = ThisWorkbook.Worksheets(2)
    
    DstRowNo = 2
    Application.ScreenUpdating = False
    
    For SrcRowNo = 2 To Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row
        Ws.Range(Ws.Cells(SrcRowNo, "A"), Ws.Cells(SrcRowNo, "C")).Copy
        
        For SrcColno = 4 To Ws.Cells(SrcRowNo, Ws.Columns.Count).End(xlToLeft).Column
            DstWs.Rows(DstRowNo).PasteSpecial
            If IsDate(Ws.Cells(SrcRowNo, "c")) Then
                DstWs.Cells(DstRowNo, "c") = DateAdd("M", SrcColno - 4, Ws.Cells(SrcRowNo, "c"))
            Else
                DstWs.Cells(DstRowNo, "c") = ""
            End If
            DstWs.Cells(DstRowNo, "D") = Ws.Cells(SrcRowNo, SrcColno)
            DstRowNo = DstRowNo + 1
        Next SrcColno
        
        DoEvents
    Next SrcRowNo
    
    Application.ScreenUpdating = True
    
    MsgBox "Complete", vbInformation
End Sub


Let me know if it is not what is expected.
 
Upvote 0
Yes! This is absolutely what I need! My apologies for the delay in responding, I've been traveling all day. You've also helped me see what I was doing wrong on my part. You are amazing thank you so much. Problem Solved!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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