VBA or Power Query or combo to unpivot my table

gaosiyou

New Member
Joined
May 19, 2016
Messages
3
Hi, I've been tring to make my file lighter on code and powerqueries to speed things up. my source table has over 40 columns and thousands of rows but for simplicity's skae, let's say I have the following table


IDColumnArrayColumnArray2
1​
ae
2​
bf
3​
bg
4​
c
5​
dh
6​
dg
7​
di

Where both Arrays have same columns but with different names (and different values). I'm trying to finish with a table with the following configuration
IDColumnArray
1.1a
1.2e
2.1b
2.2f
3.1b
3.2g
4.1c
5.1d
5.2h
5.3g
5.4i

Also have an additional constraint that I shouldn't modify the raw values in any way. For example, I haven't been able to copy paste "001" as it should stay like that and not become "1". and I haven't been able to do that with copy.
So far however, I've managed to get it done although rather clumsily in VBA with If statements. and with destrange("a1") = sourcerange.cell(1,1) lines for each column in each array, looping through the source data rows.

Any Ideas on something prettier / smoother ?

Thank you
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I know that you can unpivot using Power Query or through VBA as shown below
VBA Code:
Sub test()

Dim a
a = [a1].CurrentRegion '<-- assuming your data starts from cell A1
ReDim b(1 To UBound(a) * UBound(a, 2), 1 To 2)

For x = 2 To UBound(a)
    For y = 2 To UBound(a, 2)
        If a(x, y) <> "" Then
            i = i + 1: ii = ii + 1
            b(i, 1) = a(x, 1) & "." & ii
            b(i, 2) = a(x, y)
        End If
    Next
    ii = 0
Next

With [H2].Resize(i, 2)
    .NumberFormat = "@"
    .Value = b
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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