Data Copy and Transpose

decent_boy

Board Regular
Joined
Dec 5, 2014
Messages
130
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Data below in Sheet1 needs to be transposed manually to Result Sheet one by one, so I need a macro to automate this process

test.xlsm
ABCDEFG
1STYLE NUMBERSIZING
2XSSMLXLXXL
3S243001G11111
4S243002L111111
5S243003W11111
6TEESSMLXLXXL
7S241001B11111
8S241002T11111
9PANTSSMLXLXXL
10S244051B11111
11S244052G11111
12SWEATERSXSSMLXLXXL
13S247001A111111
14JACKETSO/S
15S248001B1
16HATSO/S
17C995005B1
Sheet1


The Result I want is like that

test.xlsm
AB
1SKUQTY
2S243001G-S1
3S243001G-M1
4S243001G-L1
5S243001G-XL1
6S243001G-XXL1
7S243003W-S1
8S243003W-M1
9S243003W-L1
10S243003W-XL1
11S243003W-XXL1
12S241001B-S1
13S241001B-M1
14S241001B-L1
15S241001B-XL1
16S241001B-XXL1
17S241002T-S1
18S241002T-M1
19S241002T-L1
20S241002T-XL1
21S241002T-XXL1
22S244051B-S1
23S244051B-M1
24S244051B-L1
25S244051B-XL1
26S244051B-XXL1
27S244052G-S1
28S244052G-M1
29S244052G-L1
30S244052G-XL1
31S244052G-XXL1
32S247001A-XS1
33S247001A-S1
34S247001A-M1
35S247001A-L1
36S247001A-XL1
37S247001A-XXL1
38S248001B-O/S1
39C995005B-O/S1
Result
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try the following on a copy of your workbook.
VBA Code:
Option Explicit
Sub Decent_Boy()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ *** Change sheet names to suit ***
    Set ws2 = Worksheets("Result")
    Dim LRow As Long, LCol As Long
    LRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    
    Dim a, b, i As Long, j As Long, k As Long, n As Long, x As Long
    a = ws1.Range(ws1.Cells(2, 1), ws1.Cells(LRow, LCol))
    x = WorksheetFunction.CountA(ws1.Range(ws1.Cells(2, 1), ws1.Cells(LRow, LCol)))
    ReDim b(1 To x, 1 To 2)
    k = 1: n = 1
    For i = 1 To UBound(a, 1)
        For j = 2 To UBound(a, 2)
            If (a(i, j)) <> "" And IsNumeric(a(i, j)) Then
                Do Until (a(i - n, j)) <> "" And IsNumeric(a(i - n, j)) = False
                    n = n + 1
                Loop
                b(k, 1) = a(i, 1) & "-" & a(i - n, j)
                b(k, 2) = a(i, j)
                n = 1
                k = k + 1
            End If
        Next j
    Next i
    
    With ws2
        .Range("A:B").ClearContents
        .Range("A1").Resize(, 2).Value = Array("SKU", "QTY")
        .Range("A2").Resize(UBound(b, 1), 2).Value = b
        .Range("A:A").EntireColumn.AutoFit
    End With
    
End Sub
 
Upvote 0
An alternative solution is to unpivot your data with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([S] <> "S")),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filtered Rows", {"Column1"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Other Columns",{"Column1", "Attribute"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"SKU"),
    #"Renamed Columns" = Table.RenameColumns(#"Merged Columns",{{"Value", "QTY"}}),
    #"Filtered Rows1" = Table.SelectRows(#"Renamed Columns", each ([QTY] = 1))
in
    #"Filtered Rows1"
 
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,254
Members
449,093
Latest member
Vincent Khandagale

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