如何“平坦化”或“崩溃”将2D Excel表格转换为1D? [英] How to "flatten" or "collapse" a 2D Excel table into 1D?

查看:183
本文介绍了如何“平坦化”或“崩溃”将2D Excel表格转换为1D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel中有一个国家和地区的二维表格。例如。

  1961 1962 1963 1964 
美国axgy
法国ueha
德国oxnp

我想扁平化,以至于我在第一列中有国家,第二年的年份,然后在第三列中值。例如,

 国家年价值
美国1961年a
美国1962年x
美国1963年
美国1964年y
法国1961年
...

我在这里提供的例子只是一个3x4的矩阵,但是我真正的数据集显着更大(大约是50x40左右)。



Excel?

解决方案

您可以使用excel数据透视表功能来反转一个数据透视表(这基本上是你在这里)



这里的说明:



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



哪些链接以下VBA代码(放在一个模块中),如果你不想遵循手动说明:

  Sub ReversePivotTable()
'运行前,请确保您有一个汇总表,列标题。
'输出表将有三列。
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
如果SummaryTable.Count = 1或SummaryTable.Rows.Count< 3然后
MsgBox选择摘要表中的单元格,vbCritical
退出子
结束If
SummaryTable.Select
设置OutputRange = Application.InputBox(提示符:=选择3列输出的单元格,类型:= 8)
'转换范围
OutRow = 2
Application.ScreenUpdating = False
OutputRange.Range (A1:C3)= Array(Column1,Column2,Column3)
对于r = 2到SummaryTable.Rows.Count
对于c = 2到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
下一步c
下一步r
结束Sub

-Adam


I have a two dimensional table with countries and years in Excel. eg.

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

I'd like to "flatten" it, such that I have Country in the first col, Year in the second col, and then value in the third col. eg.

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

The example I present here is only a 3x4 matrix, but the real dataset i have is significantly larger (roughly 50x40 or so).

Any suggestions how I can do this using Excel?

解决方案

You can use the excel pivot table feature to reverse a pivot table (which is essentially what you have here):

Good instructions here:

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

Which links to the following VBA code (put it in a module) if you don't want to follow the instructions by hand:

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

这篇关于如何“平坦化”或“崩溃”将2D Excel表格转换为1D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆