Macro to copy data from one layout to another layout

JannetteChristie

Board Regular
Joined
Dec 14, 2015
Messages
130
Office Version
  1. 365
Can anyone help with this please, I am trying to write some VBA to copy the following data in my spreadsheet to move to this format.

Starting layout:


Excel 2012
CDEFGHIJKLMNO
2VR3-0510-LHMDUTYPOINT3FS5-WRAS1COMM11FRB502
3DE0
4qp-35PRESSURISATION0
5ec50CONDITIONER2DPS-WMB-EC502DPS-FMB-EC501
6cx65cSEP0
7in15-50-p-h2HIU0
8DPS-SP1DPS-NOTE1
9
10
11
12
13
14
Save & Print prepare


New layout required:

Excel 2012
TU
2VR3-0510-LHM
3FS5-WRAS1
4COMM11
5FRB502
6DE
7QP-35
8EC50
9DPS-WMB-EC502
10DPS-FMB-EC501
11cx65c
12IN15-50-P-H2
13DPS-SP
14DPS-NOTE1
Save & Print prepare


I am sure this is possible but not sure how to code this.
 

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
How about
Code:
Sub Test()
   Dim ary As Variant, nary As Variant
   Dim r As Long, c As Long, rr As Long
   
   ary = Range("C2", Range("C" & Rows.Count).End(xlUp).Offset(, 8)).Value2
   ReDim nary(1 To UBound(ary) + Application.SUM(Range("E:E")), 1 To 2)
   For r = 1 To UBound(ary)
      rr = rr + 1
      nary(rr, 1) = ary(r, 1)
      If ary(r, 3) > 0 Then
         For c = 1 To ary(r, 3) * 2 Step 2
            rr = rr + 1
            nary(rr, 1) = ary(r, c + 3)
            nary(rr, 2) = ary(r, c + 4)
         Next c
      End If
   Next r
   Range("T2").Resize(rr, 2).Value = nary
   
End Sub
 
Last edited:
Upvote 0
Hi, I have just used this code and it works fine for most of it. However when it gets to the end and finds that ary,3="" I get the error message type mismatch. How do I get over it please?
 
Upvote 0
You wouldn't get that if the cell in col E was blank.
Do you have text in there?
 
Upvote 0
Hi,

I have added an extra column in between D & E, and am trying to also include the data in the new column in the array - ary,4. I have tried to amend the code but some of the data appears to be overwritten and I am not sure how to resolve this.

ary = Range("C2", Range("C" & Rows.Count).End(xlUp).Offset(, 14)).Value2
ReDim nary(1 To UBound(ary) + Application.Sum(Range("G:G")), 1 To 2)
For r = 1 To UBound(ary)
rr = rr + 1
nary(rr, 1) = ary(r, 1)
'nary(rr, 2) = ary(r, 2)

If ary(r, 4) > 0 And ary(r, 4) <> "" Then
nary(rr, 1) = ary(r, 4)
End If

If ary(r, 5) > 0 And ary(r, 5) <> "" Then
For c = 1 To ary(r, 5) * 2 Step 2
rr = rr + 1
nary(rr, 1) = ary(r, c + 3)
nary(rr, 2) = ary(r, c + 4)
Next c
End If
Next r
Range("U2").Resize(rr, 2).Value = nary
 
Upvote 0
Can you please post an example of your new data layout & what you are trying to do?
 
Upvote 0

Excel 2012
CDEFGHIJKLM
2VR3-1006-LHT1DUTYPOINTRECOMMENDED EXTRAS3FRB501FRB651FS5-WRAS1
3EC501CONDITIONERRECOMMENDED EXTRAS2DPS-WMB-EC502DPS-FMB-EC501
4QP-351PRESSURISATION0
5Z500LV10
6E502070AA10
7DPS-SP11DPS-NOTE1
8VR2-1005-LHT1DUTYPOINTRECOMMENDED EXTRAS3FRB651FRB501FS5-WRAS1
9VR4-0506-LHM10
Save & Print prepare


My aim is to get this:

Excel 2012
UV
2VR3-1006-LHT
3RECOMMENDED EXTRAS
4FRB501
5FRB651
6FS5-WRAS1
7EC50
8RECOMMENDED EXTRAS
9DPS-WMB-EC502
10DPS-FMB-EC501
11QP-35
12Z500LV
13E502070AA
14DPS-SP
15DPS-NOTE1
16VR2-1005-LHT
17RECOMMENDED EXTRAS
18FRB651
19FRB501
20FS5-WRAS1
21VR4-0506-LHM
Save & Print prepare
 
Upvote 0
Ok, how about
Code:
Sub Test()
   Dim ary As Variant, nary As Variant
   Dim r As Long, c As Long, rr As Long
   
   ary = Range("C2", Range("C" & Rows.Count).End(xlUp).Offset(, 10)).Value2
   ReDim nary(1 To UBound(ary) + Application.SUM(Range("G:G")) + Application.Count(Range("G:G")), 1 To 2)
   For r = 1 To UBound(ary)
      rr = rr + 1
      nary(rr, 1) = ary(r, 1)
      If ary(r, 5) > 0 Then
         rr = rr + 1
         nary(rr, 1) = ary(r, 4)
         For c = 1 To ary(r, 5) * 2 Step 2
            rr = rr + 1
            nary(rr, 1) = ary(r, c + 5)
            nary(rr, 2) = ary(r, c + 6)
         Next c
      End If
   Next r
   Range("U2").Resize(rr, 2).Value = nary
   
End Sub
 
Upvote 0
Hi,

I have amended the code as per above, but keep getting Run-time error '9': Subscript out of range.

rr=110
r=97
 
Upvote 0
When you get the error, click debug & then in the immediate window type
Code:
?ubound(nary)
&hit enter. What value does it give?
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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