Comment "aplatir" ou "réduire" un tableau Excel 2D en 1D ?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment "aplatir" ou "réduire" un tableau Excel 2D en 1D ?

Message par ForumBot »

J’ai un tableau bidimensionnel avec des pays et des années dans Excel. Par exemple :


        1961        1962        1963        1964
USA a x g y
France u e h a
Germany o x n p


J’aimerais l’“aplatir”, de sorte que j’aie le pays dans la première colonne, l’année dans la deuxième colonne, puis la valeur dans la troisième colonne. Par exemple :


Country      Year       Value
USA 1961 a
USA 1962 x
USA 1963 g
USA 1964 y
France 1961 u
...


L’exemple que je présente ici n’est qu’une matrice 3x4, mais le vrai jeu de données que j’ai est significativement plus grand (environ 50x40).


Des suggestions sur comment faire cela avec Excel ?




Source : [Stack Overflow](http://spreadsheetpage.com/index.php/tip/creating_a_database_table_from_a_summary_table/](http://spreadsheetpage.com/index.php/tip/creating_a_database_table_from_a_summary_table/)

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment "aplatir" ou "réduire" un tableau Excel 2D en 1D ?

Message par ayi »

Vous pouvez utiliser la fonctionnalité de tableau croisé dynamique d’Excel pour inverser un tableau croisé dynamique (ce qui est essentiellement ce que vous avez ici) :


De bonnes instructions ici :


http://spreadsheetpage.com/index.php/tip/creating_a_database_table_from_a_summary_table/


Qui renvoie au code VBA suivant (mettez-le dans un module) si vous ne souhaitez pas suivre les instructions manuellement :


Sub ReversePivotTable()
' Before running this, make sure you have a summary table with column headers.
' The output table will have three columns.
Dim SummaryTable As Range, OutputRange As Range
Dim OutRow As Long
Dim r As Long, c As Long

On Error Resume Next
Set SummaryTable = ActiveCell.CurrentRegion
If SummaryTable.Count = 1 Or SummaryTable.Rows.Count < 3 Then
MsgBox "Select a cell within the summary table.", vbCritical
Exit Sub
End If
SummaryTable.Select
Set OutputRange = Application.InputBox(prompt:="Select a cell for the 3-column output", Type:=8)
' Convert the range
OutRow = 2
Application.ScreenUpdating = False
OutputRange.Range("A1:C3") = Array("Column1", "Column2", "Column3")
For r = 2 To SummaryTable.Rows.Count
For c = 2 To SummaryTable.Columns.Count
OutputRange.Cells(OutRow, 1) = SummaryTable.Cells(r, 1)
OutputRange.Cells(OutRow, 2) = SummaryTable.Cells(1, c)
OutputRange.Cells(OutRow, 3) = SummaryTable.Cells(r, c)
OutputRange.Cells(OutRow, 3).NumberFormat = SummaryTable.Cells(r, c).NumberFormat
OutRow = OutRow + 1
Next c
Next r
End Sub


-Adam

Répondre

Revenir à « Excel & VBA »