Reverse Pivot Macro

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
338
Office Version
  1. 365
Platform
  1. Windows
Hi ,
I need a reverse macro pivot for this case, no headers, just column A items repeated for each value in respective rows

ex1AQTBTGATOARIAVLADRAFRAKOASHALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex2AQTBTGATOARIAVLADRAFRAKO
ex3ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex4ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex5ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex6ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex7ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex8ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex9ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex10ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex11ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex12ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex13ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex14ALUALEITGITOIMSIVLIOXIDRISHIFGICPOOXALIOLAOMOIMO
ex15ALU
ex16ALU
ex17ALUALEITGITOIMSIVL
ex18ALUALEITGITOIMSIVL
ex19ALUALEITGITOIMSIVL
ex20ALUALEITGITOIMSIVL
ex21ALUALEITGITOIMSIVL
ex22ALUALEITGITOIMSIVL
ex23ALUALEITGITOIMSIVL
ex24ALUALEITGITOIMSIVL
ex25ISHIFGICPOOXALIOLAOMOIMO
ex26ISHIFGICPOOXALIOLAOMOIMO
ex27ISHIFGICPOOXALIOLAOMOIMO
ex28ISHIFGICPOOXALIOLAOMOIMO
ex29ISHIFGICPOOXALIOLAOMOIMO
ex30ISHIFGICPOOXALIOLAOMOIMO
ex31ISHIFGICPOOXALIOLAOMOIMO
ex32ISHIFGICPOOXALIOLAOMOIMO
ex33ISHIFGICPOOXALIOLAOMOIMO
ex34ISHIFGICPOOXALIOLAOMOIMO
 

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.
Unpivoting macro proposition:
assuming the sheet is active and data starts in A1:

VBA Code:
Sub test()
Dim lr As Long, lc As Long, i As Long, j As Long, counter As Long
Dim inarr As Variant, outtarr() As Variant
lr = Cells(Rows.Count, "A").End(xlUp).Row
With Range(Cells(1, "B"), Cells(lr, Columns.Count))
  On Error Resume Next
    lc = .Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
      SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
  On Error GoTo 0
End With
inarr = Range(Cells(1, "A"), Cells(lr, lc)).Value
ReDim outarr(1 To 2, 1 To 1)
For i = 1 To lr
  For j = 2 To lc
    If Len(inarr(i, j)) > 0 Then
      counter = counter + 1
      ReDim Preserve outarr(1 To 2, 1 To counter)
      outarr(1, counter) = inarr(i, 1)
      outarr(2, counter) = inarr(i, j)
    End If
Next j, i
Cells(1, lc + 2).Resize(counter, 2) = Application.Transpose(outarr)
End Sub
 
Upvote 1
But (for many people - obviously) the easiest way to achieve such result is probably by using Power Query (Get and transform data).
I did it as separate workbook, but steps are almost the same for doing it in the same workbook:
1) start with Data->Get&transform->From file->Excel workbook (in case of dayta in the same wb - from table/range) and select source
2) click transform
3) remove 2 automatically created steps: promote headers and change type
4) (having Column 1 selected) from Transform Card use Unpivot other columns
5) remove unneeded Attribute column
6) from mail close and load to (or close and load).

That's it. And with new file you just change input data. The m-code which was created during clicking points 1-6 above) is as simple as:
Power Query:
let
    Source = Excel.Workbook(File.Contents("C:\Users\Kaper\test\test1.xlsm"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Sheet1_Sheet, {"Column1"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"})
in
    #"Removed Columns"
so much simpler than VBA


1733565662495.png
 
Upvote 1
And yet another idea, as you are using Microsoft 365 - a formula:

Excel Formula:
=LET(src,A1:AF33,first,TAKE(src,,1),cols,COLUMNS(src)-1,notfi,HSTACK(REDUCE(first,SEQUENCE(cols-1),LAMBDA(a,b,VSTACK(a,first))),TOCOL(DROP(src,,1),,TRUE)),FILTER(notfi,TAKE(notfi,,-1)<>""))
 
Upvote 1
Hi Kaper, thank you for the exhaustive reply.
In order to run it automatically which solution is more appropriate ?
 
Upvote 0
Another basic "unpivot" formula for MS365 is the TOCOL-IFS method:

by row:
Excel Formula:
=LET(test,B1:AF34<>"",HSTACK(TOCOL(IFS(test,A1:A34),2),TOCOL(IFS(test,B1:AF34),2)))

by column:
Excel Formula:
=LET(test,B1:AF34<>"",HSTACK(TOCOL(IFS(test,A1:A34),2,1),TOCOL(IFS(test,B1:AF34),2,1)))

Adjust the range references as needed.
 
Upvote 0
As for automatisation - is data in excel file? text file? - does the name of the file change? how large is original dataset, how often does it change, and some other questions ...
Probably most automatic is formula approach. Once data is entered in workbook, output is recalculated (if the manual calculation mode is not swithched on)
 
Upvote 0

Forum statistics

Threads
1,224,875
Messages
6,181,514
Members
453,050
Latest member
Obil

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