Insert # Rows based on numeric cell value and copy entire row to all inserted rows help

Keith412

New Member
Joined
Mar 2, 2008
Messages
41
Location
#
Month
Year
NYC
5
July
2019
Florida
3
August
2019
Mass
1
October
2019

<tbody>
</tbody>
I have a workbook with the above information:


I would want it to look at column B, (#) and then insert 5 rows where the #= 1, so 1,1,1,1, instead of the 5:

Location # MonthYear
New York 1 Jul2019
New York 1 Jul2019
New York 1 Jul2019
New York 1 Jul2019
New York 1 Jul2019
Florida 1 Jun2019
Florida 1 Jun2019
Florida 1 Jun2019
Florida 1 Jul2019
Massachusetts 1 Jul2019
<colgroup><col width="97" style="width: 73pt; mso-width-source: userset; mso-width-alt: 3547;"> <col width="43" style="width: 32pt; mso-width-source: userset; mso-width-alt: 1572;"> <col width="46" style="width: 35pt; mso-width-source: userset; mso-width-alt: 1682;"> <col width="36" style="width: 27pt; mso-width-source: userset; mso-width-alt: 1316;"> <tbody> </tbody>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I think your sampled Data is flawed.

In the top sample you have NYC but after script runs you want New YorK

And the same with Mass

And why does Florida and August get changed to Florida and June

And Mass from October to July
 
Last edited:
Upvote 0
You are right, when I manually manipulated the file it didn't create the outcome I wanted. Here is a simplified version of how its currently organized versus what I want to accomplish

Location#
A4
B1
C2
Location#
A1
A1
A1
A1
B1
C1
C1
<colgroup><col width="109" style="width: 82pt; mso-width-source: userset; mso-width-alt: 3986;"> <col width="74" style="width: 56pt; mso-width-source: userset; mso-width-alt: 2706;"> <tbody> </tbody>
 
Upvote 0
How about
Code:
Sub Keith412()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, rr As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * 1000, 1 To 4)
   
   For r = 2 To UBound(Ary)
      For c = 1 To Ary(r, 2)
         rr = rr + 1
         Nary(rr, 1) = Ary(r, 1)
         Nary(rr, 2) = 1
         Nary(rr, 3) = Ary(r, 3)
         Nary(rr, 4) = Ary(r, 4)
      Next c
   Next r
    Range("G1:J1").Value = Application.Index(Ary, 1, 0)
    Range("G2").Resize(rr, 4).Value = Nary
End Sub
 
Upvote 0
There you go I asked a question and before I could get back you have a answer.
 
Upvote 0
worked perfectly on my sample, but struggling to apply it to my full data set. Full dataset has 11 columns (A1 to K1) an the # is located in column "G"
 
Upvote 0
In that case try
Code:
Sub Keith412()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, j As Long, rr As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * 1000, 1 To 11)
   
   For r = 2 To UBound(Ary)
      For j = 1 To Ary(r, 7)
         rr = rr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(rr, c) = Ary(r, c)
            Nary(rr, 7) = 1
         Next c
      Next j
   Next r
    Range("M1:W1").Value = Application.Index(Ary, 1, 0)
    Range("M2").Resize(rr, 11).Value = Nary
End Sub
 
Upvote 0
Well this is how I would have done it:
Not sure what you new request wants.
Code:
Sub Insert_Rows()
'Modified 12/20/2018 11:40:02 AM  EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = Lastrow To 2 Step -1
    Rows(i).Offset(1).Resize((Cells(i, 2).Value) - 1).Insert
    Rows(i).Offset(1).Resize((Cells(i, 2).Value) - 1).Value = Rows(i).Value
Next
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Cells(2, 2).Resize(Lastrow - 1).Value = "1"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
What error message do you get?
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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