简体   繁体   English

在 Excel 中将具有多行数据的列转换为具有多列的行。

[英]Convert columns with multiple rows of data to rows with multiple columns in Excel.

I have data organised in cloumns with multiple rows which I need to convert to rows with multiple columns for data analysis.我将数据组织在多行的 cloumns 中,我需要将其转换为多列的行以进行数据分析。 For example,例如,

    ID  Date of entry   Location    Data1   Data2
    1   20101030        1           a       b
    1   20101030        2           c       d
    1   20101125        1           w       v
    1   20101125        2           e       d
    1   20110314        1           we      r
    1   20110314        2           f       f
    2   20091024        1           ewr     rte
    2   20091024        2           gr      ert
    3   20061128        1           jy      bf
    3   20061128        2           u       df
    3   20110517        1           rd      fd
    3   20110517        2           sg      sd

into this format with each ID row with multiple columns of data (only head row shown below)将每个 ID 行转换为具有多列数据的格式(下面仅显示首行)

ID  entry_1 Dateofentry location_1  data1   data2   location_2  data1   data2   entry_2 Dateofentry location_1  data1   data2   location_2  data1   data2   entry_3 Dateofentry location_1  data1   data2   location_2  data1   data2

Can anyone assist?任何人都可以提供帮助吗?

Thanks!谢谢! GT GT

You'll have to add the headers yourself, but this code should do what you need:您必须自己添加标题,但此代码应该可以满足您的需求:

Sub ConsolidateRows_SpreadAcross()

Dim lastRow As Long, i As Long, j As Long
Dim colMatch As Variant, colConcat As Variant

application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes

lastRow = range("A" & Rows.Count).End(xlUp).Row 'get last row

For i = lastRow To 2 Step -1

    If Cells(i, 2) = Cells(i - 1, 2) Then
        range(Cells(i, 3), Cells(i, Columns.Count).End(xlToLeft)).Copy Cells(i - 1, Columns.Count).End(xlToLeft).Offset(, 1)
        Rows(i).Delete
    Else
        If Cells(i, 1) = Cells(i - 1, 1) Then
            range(Cells(i, 2), Cells(i, Columns.Count).End(xlToLeft)).Copy _
                Cells(i - 1, Columns.Count).End(xlToLeft).Offset(, 1)
            Rows(i).Delete
        End If
    End If

Next

application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub

If you copy the whole table, and then right-click wherever you want to paste the new data, you should be given the menu "Paste Special."如果您复制整个表格,然后右键单击要粘贴新数据的任何位置,您应该会看到“选择性粘贴”菜单。 From that menu, choose transpose.从该菜单中,选择转置。 That should transpose the columns into rows.这应该将列转换为行。

I started from a formula I found on the internet that takes one-column list and changes it's layout to multyple columns: that insteat of 1000+ rows you'll get 60 rows with many columns of that list.我从我在互联网上找到的一个公式开始,该公式采用一列列表并将其布局更改为多列:在 1000 多行的情况下,您将获得 60 行,其中包含该列表的多列。 you can find it here http://help.lockergnome.com/office/list-multiple-columns-page--ftopict935389.html你可以在这里找到它http://help.lockergnome.com/office/list-multiple-columns-page--ftopict935389.html

i wanted to take a list that has 4 columns (the 5th column is empty) and 1000+ rows, and make it a 50 rows table that the 4 fields repeats itself.我想要一个有 4 列(第 5 列是空的)和 1000 多行的列表,并使它成为一个 50 行的表,其中 4 个字段重复。

for example i have:例如我有:

family  |  name   |  amount  |   table |
fam1    |  shlomi |  2       |   38    |
fam2    |  hila   |  4       |   23    |    
....    
fam1000 |  avi    |  1       |   15    |

and i want to make it我想成功

family  |  name   |  amount  |   table |     |fam50    |  ben   |  2       |   68    |  ...    
fam1    |  shlomi |  2       |   38    |      ...    
fam2    |  hila   |  4       |   23    |      ...    
...                                           ...    
fam49   |  dror   |  1       |   15    |     |fam99    |  dror   |  1      |   15    | ...

On a new worksheet in your existing workbook, insert the following formula into A1:在现有工作簿中的新工作表上,将以下公式插入 A1:

=IF(OFFSET(Sheet1!$A$1,QUOTIENT(COLUMN(),5)*50+ROW()-1,MOD(COLUMN()-1,5))="","",OFFSET(Sheet1!$A$1,QUOTIENT(COLUMN(),5)*50+ROW()-1,MOD(COLUMN()-1,5))) =IF(OFFSET(Sheet1!$A$1,QUOTIENT(COLUMN(),5)*50+ROW()-1,MOD(COLUMN()-1,5))="","",OFFSET(Sheet1! $A$1,QUOTIENT(COLUMN(),5)*50+ROW()-1,MOD(COLUMN()-1,5)))

.. Copy this formula across as many columns as you need, and as many rows as you need ( I used 50 in my example) .. 根据需要在尽可能多的列和尽可能多的行中复制此公式(我在示例中使用了 50)

You can change '5' in the furmula to the number of fields you want in each column layout, and you can change '50' to the number of rows you want in each page.您可以将 furmula 中的“5”更改为每个列布局中所需的字段数,您可以将“50”更改为每个页面中所需的行数。

family |家庭 | name |姓名 | amount |金额 | table |表| fam1 | fam1 | shlomi |什洛米 | 2 | 2 | 38 | 38 | fam2 | fam2 | hila |希拉| 4 | 4 | 23 | 23 | .... fam1000 | .... fam1000 | avi | AVI | 1 | 1 | 15 | 15 | fam50 | fam50 | ben |本 | 2 | 2 | 68 | 68 |



dror |滴滴| 1 | 1 | 15 | 15 |

and i want to make it我想成功

family |家庭 | name |姓名 | amount |金额 | table |表| |fam50 | |fam50 | ben |本 | 2 | 2 | 68 | 68 | ... ...
fam1 | fam1 | shlomi |什洛米 | 2 | 2 | 38 | 38 | ... ...
fam2 | fam2 | hila |希拉| 4 | 4 | 23 | 23 | ... ...
... ... ……
fam49 | fam49 | avi | AVI | 1 | 1 | 15 | 15 | |fam99 | |fam99 | dror |滴滴| 1 | 1 | 15 | 15 | ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM