Unpivot an Excel matrix/pivot-table?

前端 未结 4 1023
慢半拍i
慢半拍i 2020-12-07 04:28

Is there a quick way to \"unpivot\" an Excel matrix/pivot-table (in Excel or elsewhere), without writing macros or other code ?
Again, I can write co

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 05:20

    I believe that you can use a sort of modular arithmetic as follows. Put your data into argument of this UDF with cols and rows legend.

    Function MyUnpivot(matice As Range) As Variant
        Dim I As Integer
        Dim J As Integer
    
        Dim radka As Integer
        Dim sloupec As Integer
    
        I = matice.Rows.Count - 1
        J = matice.Columns.Count - 1
    
        Dim returnVal()
        ReDim Preserve returnVal(1 To I * J, 1 To 3)
    
        For x = 1 To I * J
            radka = ((x - 1) Mod I) + 2
            sloupec = WorksheetFunction.Floor_Math((x - 1 / 2) / I) + 2
            returnVal(x, 1) = matice.Cells(1, sloupec)
            returnVal(x, 2) = matice.Cells(radka, 1)
            returnVal(x, 3) = matice.Cells(radka, sloupec)
        Next
    
        MyUnpivot = returnVal
    End Function
    

提交回复
热议问题