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

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
677
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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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
 

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
677
Office Version
  1. 2016
Platform
  1. MacOS
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?
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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:

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
677
Office Version
  1. 2016
Platform
  1. MacOS
That worked great! Thanks for all the help on this.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,544
Messages
5,636,920
Members
416,952
Latest member
Dougiefletcher

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