Excel VBA to create multi rows based on column header

Stildawn

Board Regular
Joined
Aug 26, 2012
Messages
197
Hi All

I have this sort of data below:

Data Example.JPG


I'm running For Next to go through the data row by row, what I need to do on each row is:

- Create a new row on a separate sheet with the "Style" combined with size (red columns) into A​
- Next to the new row, put the number under that size​
- Next to that use the "unit price" times the number to get total value​
- Repeat the above again for the next red size column and continue until all red columns are done.​
For example, the first row of data above (in row 2), I would need the new sheet to look like this:
StyleQTYValue
ABC123-S15$90
ABC123-M20$120
ABC123-L20$120
ABC123-XL30$180
ABC123-XXL20$120

And then the next sytle "ABC124" below that, again with all the sizes added on.

Hope this makes sense, basically I need to create style codes for each style + size, and then list the QTY and the total value, but over hundreds of lines.

Is there a easy VBA way to do this? I'm sure I could code it out, but my code would be super long and complicated haha.

Thanks in advance.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try this code:

VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, j&, k&, rng, res(1 To 100000, 1 To 3)
lr = Cells(Rows.Count, "A").End(xlUp).Row
rng = Range("A1:I" & lr)
For i = 2 To UBound(rng)
    For j = 2 To 8
        If rng(i, j) > 0 Then
            k = k + 1
            res(k, 1) = rng(i, 1) & "-" & rng(1, j)
            res(k, 2) = rng(i, j)
            res(k, 3) = rng(i, j) * rng(i, 9)
        End If
    Next
Next
If k > 0 Then Range("K2").Resize(k, 3).Value = res ' adjust destination as needed
End Sub
Book1
ABCDEFGHIJKLM
1XSSMLXLXXLXXXLU.P
2ABC1231520183035406ABC123-S1590
3ABC1241621193136417ABC123-M20120
4ABC1251722203237428ABC123-L18108
5ABC1261823213338439ABC123-XL30180
6ABC127619242234394410ABC123-XXL35210
7ABC12820252335404511ABC123-XXXL40240
8ABC12921262436414612ABC124-S16112
9ABC130322272537424713ABC124-M21147
10ABC13123282638434814ABC124-L19133
11ABC13224292739444915ABC124-XL31217
12ABC13325302840455016ABC124-XXL36252
13ABC124-XXXL41287
14ABC125-S17136
15ABC125-M22176
16ABC125-L20160
17ABC125-XL32256
18ABC125-XXL37296
19ABC125-XXXL42336
20ABC126-S18162
21ABC126-M23207
22ABC126-L21189
23ABC126-XL33297
24ABC126-XXL38342
25ABC126-XXXL43387
26ABC127-XS660
27ABC127-S19190
28ABC127-M24240
29ABC127-L22220
30ABC127-XL34340
31ABC127-XXL39390
32ABC127-XXXL44440
33ABC128-S20220
34ABC128-M25275
35ABC128-L23253
36ABC128-XL35385
37ABC128-XXL40440
38ABC128-XXXL45495
39ABC129-S21252
40ABC129-M26312
41ABC129-L24288
42ABC129-XL36432
43ABC129-XXL41492
44ABC129-XXXL46552
45ABC130-XS339
46ABC130-S22286
47ABC130-M27351
48ABC130-L25325
49ABC130-XL37481
50ABC130-XXL42546
51ABC130-XXXL47611
52ABC131-S23322
53ABC131-M28392
54ABC131-L26364
55ABC131-XL38532
56ABC131-XXL43602
57ABC131-XXXL48672
58ABC132-S24360
59ABC132-M29435
60ABC132-L27405
61ABC132-XL39585
62ABC132-XXL44660
63ABC132-XXXL49735
64ABC133-S25400
65ABC133-M30480
66ABC133-L28448
67ABC133-XL40640
68ABC133-XXL45720
69ABC133-XXXL50800
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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