VBA Help - Create a Duplicate row based on Cell Value - Excel 2016

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I have a report that has data in a Column based format (Left to right) that I need to convert into a linear format (up and down) and not sure that fastest way to accomplish this.

My data has column detail from Columns A:F, in in Columns G:AP there are ID's that I need to grab and create a duplicate of the row and enter each unique ID into column F. I did create a helper column in Column E that Counts how many values are in Columns G:AP.

Once the code runs it will clear all the values in columns G:AP since they have now been moved into Column F as unique rows.

Currently my report has 121 rows of data and for each row so will definitely need code to achieve this quickly.

Here is a mocked-up example of what I mean;

Current (I intentionally left columns B:E out of the grid for this example)
Col ACol ECol FID1ID2ID3ID4
Name13Currently BlankJ1J2J3
Name22Currently BlankZ5Z10

<tbody>
</tbody>






Expectation
Col ACol ECol FID1ID2ID3ID4
Name 13J1
Name 13J2
Name 13J3
Name 22Z5
Name 22Z10

<tbody>
</tbody>











Any help on this is appreciated.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this

Code:
Sub t()
Dim i As Long, cnt As Long, rng As Range
With ActiveSheet
    For i = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        cnt = .Cells(i, 5).Value
        Set rng = .Cells(i, 7).Resize(, cnt)
        .Cells(i + 1, 1).Resize(cnt - 1).EntireRow.Insert
        .Cells(i, 1).Resize(cnt) = .Cells(i, 1).Value
        rng.Copy
        .Cells(i, 6).PasteSpecial xlPasteValues, Transpose:=True
        rng.ClearContents
    Next
End With
End Sub
 
Upvote 0
Code seems to work ok but I am having an issue since the code is defining Column(1) as the column to define how many rows to work with, my column(1) has several blanks in it. How would I modify to use column E as the column to define row count?
 
Upvote 0
Code:
Sub t2()
Dim i As Long, cnt As Long, rng As Range
With ActiveSheet
    For i = .Cells(Rows.Count, 5).End(xlUp).Row To 2 Step -1
        cnt = .Cells(i, 5).Value
        Set rng = .Cells(i, 7).Resize(, cnt)
        .Cells(i + 1, 1).Resize(cnt - 1).EntireRow.Insert
        If .Cells(i, 1) <> "" Then
            .Cells(i, 1).Resize(cnt) = .Cells(i, 1).Value
        End If
        rng.Copy
        .Cells(i, 6).PasteSpecial xlPasteValues, Transpose:=True
        rng.ClearContents
    Next
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,524
Messages
6,114,117
Members
448,549
Latest member
brianhfield

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