Insert blank rows based on a number, then copy/paste values

BigBee11

New Member
Joined
Mar 28, 2018
Messages
3
Hello Excel Pros,

I've somewhat unique excel problem and it's taking a lot of my time because I do it pretty much manually. I'm asking all the pros to help me figure out a quicker way to get this done. Below is how I get data in excel sheet:

company#of ficescity1city2city3city4city5city6city7city1-PRcity1-NPcity1-AScity1-OLcity2-PRcity2-NPcity2-AScity2-OLcity3-PRcity3-NPcity3-AScity3-OLcity4-PRcity4-NPcity4-AScity4-OLcity5-PRcity5-NPcity5-AScity5-OLcity6-PRcity6-NPcity6-AScity6-OLcity7-PRcity7-NPcity7-AScity7-OL
company15Baton Rouge, LouisianaNew Orleans, LouisianaShreveport, LouisianaLake Charles, LouisianaSouth Baton Rouge, Louisiana 620369170152601010002000
company23Chicago, ILDurham, NCMountain View, CA392121200000000
company37Miami, FloridaTampa, FloridaFort Lauderdale, FloridaOrlando, FloridaJacksonville, FloridaKingston, JamaicaNassau, The Bahamas213102145000000000001000010100

<colgroup><col style="mso-width-source:userset;mso-width-alt:9472;width:194pt" width="259"> <col style="width:48pt" span="36" width="64"> </colgroup><tbody>
</tbody>


And how the final output would look:

CompanyCITYPRNPASOL
company1Baton Rouge, Louisiana620369
company1New Orleans, Louisiana170152
company1Shreveport, Louisiana6010
company1Lake Charles, Louisiana1000
company1South Baton Rouge, Louisiana 2000
company2Chicago, IL3921212
company2Durham, NC0000
company2Mountain View, CA0000
company3Miami, Florida213102
company3Tampa, Florida1450
company3Fort Lauderdale, Florida0000
company3Orlando, Florida0000
company3Jacksonville, Florida0010
company3Kingston, Jamaica0001
company3Nassau, The Bahamas0100

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>

I need to insert blank rows based on #of offices and copy/paste each city data per row. what would be the best/easiest way to do this? Thanks in advance for your help!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi & welcome to MrExcel.

How about this, it will put the data on a new sheet.
Code:
Sub RearrangeData()

   Dim i As Long, j As Long, k As Long
   Dim Rws As Long
   Dim NxtRw As Long
   Dim Ws As Worksheet
   
   Set Ws = Sheets("[COLOR=#ff0000]Output[/COLOR]")
   Ws.Range("A1:F1").Value = Array("Company", "CITY", "PR", "NP", "AS", "OL")
   With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
      For i = 2 To .Range("A" & Rows.Count).End(xlUp).Row
         Rws = .Range("B" & i).Value
         NxtRw = Ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
         Ws.Range("A" & NxtRw).Resize(1 * Rws).Value = .Range("A" & i).Value
         Ws.Range("B" & NxtRw).Resize(Rws).Value = Application.Transpose(.Range("C" & i).Resize(, Rws))
         k = 10
         For j = 1 To Rws
            Ws.Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(, 4).Value = .Cells(i, k).Resize(, 4).Value
            k = k + 4
         Next j
      Next i
   End With
End Sub
Change sheet names in red to suit
 
Upvote 0
Thank you very much. I have data in more than 300 rows and 400 columns with many blanks cells. When I tried this, I didn't correctly copies and pasted the values. How can I modify this to work for large number of rows and columns. Thanks!

Hi & welcome to MrExcel.

How about this, it will put the data on a new sheet.
Code:
Sub RearrangeData()

   Dim i As Long, j As Long, k As Long
   Dim Rws As Long
   Dim NxtRw As Long
   Dim Ws As Worksheet
   
   Set Ws = Sheets("[COLOR=#ff0000]Output[/COLOR]")
   Ws.Range("A1:F1").Value = Array("Company", "CITY", "PR", "NP", "AS", "OL")
   With Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
      For i = 2 To .Range("A" & Rows.Count).End(xlUp).Row
         Rws = .Range("B" & i).Value
         NxtRw = Ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
         Ws.Range("A" & NxtRw).Resize(1 * Rws).Value = .Range("A" & i).Value
         Ws.Range("B" & NxtRw).Resize(Rws).Value = Application.Transpose(.Range("C" & i).Resize(, Rws))
         k = 10
         For j = 1 To Rws
            Ws.Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(, 4).Value = .Cells(i, k).Resize(, 4).Value
            k = k + 4
         Next j
      Next i
   End With
End Sub
Change sheet names in red to suit
 
Upvote 0
Without an accurate sample of your sheet I can't tell.
 
Upvote 0
How about
Code:
Sub RearrangeData()

   Dim i As Long, j As Long, k As Long
   Dim Rws As Long
   Dim NxtRw As Long
   Dim ws As Worksheet
   
   Set ws = Sheets("Output")
   ws.Range("A1:F1").Value = Array("Company", "CITY", "PR", "NP", "AS", "OL")
   With Sheets("Flip")
      For i = 2 To .Range("A" & Rows.Count).End(xlUp).Row
         Rws = .Range("B" & i).Value
         NxtRw = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
         ws.Range("A" & NxtRw).Resize(1 * Rws).Value = .Range("A" & i).Value
         ws.Range("B" & NxtRw).Resize(Rws).Value = Application.Transpose(.Range("C" & i).Resize(, Rws))
         k = 83
         For j = 1 To Rws
            ws.Range("C" & NxtRw).Resize(, 4).Value = .Cells(i, k).Resize(, 4).Value
            NxtRw = NxtRw + 1
            k = k + 4
         Next j
      Next i
   End With
End Sub
 
Upvote 0
GREAT, this works, many thanks!


How about
Code:
Sub RearrangeData()

   Dim i As Long, j As Long, k As Long
   Dim Rws As Long
   Dim NxtRw As Long
   Dim ws As Worksheet
   
   Set ws = Sheets("Output")
   ws.Range("A1:F1").Value = Array("Company", "CITY", "PR", "NP", "AS", "OL")
   With Sheets("Flip")
      For i = 2 To .Range("A" & Rows.Count).End(xlUp).Row
         Rws = .Range("B" & i).Value
         NxtRw = ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
         ws.Range("A" & NxtRw).Resize(1 * Rws).Value = .Range("A" & i).Value
         ws.Range("B" & NxtRw).Resize(Rws).Value = Application.Transpose(.Range("C" & i).Resize(, Rws))
         k = 83
         For j = 1 To Rws
            ws.Range("C" & NxtRw).Resize(, 4).Value = .Cells(i, k).Resize(, 4).Value
            NxtRw = NxtRw + 1
            k = k + 4
         Next j
      Next i
   End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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