2 votos

Unpivot/Transpose columnas (sin espacios en blanco)

Tengo una tabla de jerarquía de activos en Excel:

enter image description here


A efectos de prueba, la tabla puede copiarse/pegarse en Excel utilizando el texto que figura a continuación:

Sugerencia: Copie el texto de Stack Overflow modo de edición --en lugar de copiar desde el modo de vista previa (el texto del modo de edición se parseará correctamente en Excel).

Hierarchy Path  Spec 1  Spec 2  Spec 3  Spec 4  Spec 5  Spec 6  Spec 7  Spec 8  Spec 9  Spec 10
Passenger Car   A   B   C   D   E   F   G   H   I   J
Sport Utility Vehicle   H   I   J   K   L   M   N   O   P   
1/2 Ton Pick-Up Truck   Q   R   S   T   U   V   W   X       
3/4 Ton Pick-Up Truck   Y   Z   A   B   C   D   E           
Compact Van F   G   H   I   J   K               
Cargo Van   L   M   N   O   P                   
Light Duty Truck    Q   R   S   T   

Pregunta:

Para cada Ruta Jerárquica, quiero:

  • Transponer las especificaciones
  • Rellene la ruta jerárquica de cada especificación

Nota: El número de especificaciones varía según la ruta jerárquica. Me gustaría que el número de filas coincidiera con el número de especificaciones. En otras palabras, no quiero especificaciones en blanco en la salida.

Ejemplo:

enter image description here


¿Hay alguna forma de hacerlo automáticamente en Excel 2016?

4voto

Ron Rosenfeld Puntos 418

Puedes utilizar Power Query. Entonces sus usuarios pueden actualizar cosas seleccionando el Actualizar en la pestaña Datos.

En PQ Todo lo que tiene que hacer es

  • Seleccione Hierarchy Path columna
  • **UN**pivot other columns
  • Borrar la columna Atributo resultante

Todos los pasos se pueden hacer desde la interfaz de usuario, pero aquí está el

Código M

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"    Hierarchy Path", type text}, {"Spec 1", type text}, {"Spec 2", type text}, {"Spec 3", type text}, {"Spec 4", type text}, {"Spec 5", type text}, {"Spec 6", type text}, {"Spec 7", type text}, {"Spec 8", type text}, {"Spec 9", type text}, {"Spec 10", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"    Hierarchy Path"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"})
in
    #"Removed Columns"

enter image description here

3voto

User1973 Puntos 107

enter image description here


Macro VBA:

Sub TransposeRows()
    Dim wb As Workbook
    Dim Row As Integer, LastRowInput As Integer, LastColumnInput As Integer, LastRowOutput As Integer
    Dim Path As String, Spec As String

    Set wb = ThisWorkbook
    Set InputData = wb.Sheets("Input")
    Set OutputData = wb.Sheets("Output")

    LastRowInput = InputData.UsedRange.Rows.Count
    LastColumnInput = InputData.UsedRange.Columns.Count

    OutputData.Range("A:Z").Clear
    OutputData.Range("A1").Value = "Hierarchy Path"
    OutputData.Range("B1").Value = "Spec"

    For Row = 2 To LastRowInput
        Path = InputData.Cells(Row, 1).Value
        For Col = 2 To LastColumnInput
            Spec = InputData.Cells(Row, Col).Value
            If Spec <> "" Then
                LastRowOutput = OutputData.UsedRange.Rows.Count + 1
                OutputData.Cells(LastRowOutput, 1).Value = Path
                OutputData.Cells(LastRowOutput, 2).Value = Spec
            End If
        Next
    Next

    OutputData.Select
End Sub

Resultado:

enter image description here


Notas:

El script admite especificaciones en blanco.

También puede manejarlo si el usuario añade/quita filas o columnas.

EnMiMaquinaFunciona.com

EnMiMaquinaFunciona es una comunidad de administradores de sistemas en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros sysadmin, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X