VBA insert variable number of rows and copy data

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
I have data that looks like the following:

Book1
ABCDEFGHIJKLMNOPQRSTUVW
1NameDeptHead3Head4Head5Head6Head7Head8Head9Head10Head11Head12Head13Head14Head15RegOTDTother1other2other3other4other5
2KevinAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data00600000
3MaryBBBOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data80500300
4JohnCCCOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data05000000
5WillilamAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data00000090
6SusanDDDOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data50000000.5
7ChelseaAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data106100.5000
Sheet1


Below each row I need to insert a number of rows based on how many values from P to W are greater than 0.
Then, replicate the rows from columns A to O and show the column headings from P1 to W1 and the corresponding non-zero data so that the result looks like the following:

Book1
ABCDEFGHIJKLMNOPQ
10NameDeptHead3Head4Head5Head6Head7Head8Head9Head10Head11Head12Head13Head14Head15Result1Result2
11KevinAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data6DT
12MaryBBBOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data8Reg
13MaryBBBOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data5DT
14MaryBBBOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data3other3
15JohnCCCOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data5OT
16WillilamAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data9other4
17SusanDDDOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data5Reg
18SusanDDDOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data0.5other5
19ChelseaAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data10Reg
20ChelseaAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data6OT
21ChelseaAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data1DT
22ChelseaAAAOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_DataOther_Data0.5other2
Sheet1


In other words, I want to show only the non-zero values from each row's columns P through W, and indicate where they came from (Row 1 headings in P through W)
And duplicate the data (e.g. Mary's columns A through O are duplicaded twice because there are three non-zero values in P through W).

SIGH. Hope that's fairly clear.

It should be done in VBA and the resulting array of data can be on a new sheet.
 
Thanks, again, Alex for the follow-up. Really appreciate it.
I understand what you did re the new proposed code in the "ReDim"

Kevin
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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