简体   繁体   English

如何使用Excel转置数据

[英]How to Transpose Data Using Excel

is there a way that i can do the following transformation ? 有什么办法可以进行以下转换吗?

在此处输入图片说明

Nov-2010    
1195
1175
1676
36500.5
$33,607
Dec-2010    
1215
1189
1775
46866.5
$38,714

to

Nov-2010 1195   1175    1676    36500.5    $33,607
Dec-2010 1215   1189    1775    46866.5    $38,714

Do i have to fill in the blanks first ? 我必须先填写空白吗?

I am assuming that not every month has exactly five numbers. 我假设并非每个月都有五个数字。 If it does you don't need any of the extra checks and you can just paste a much simpler formula into F4:I9 (explained below). 如果是这样,则不需要任何额外的检查,您只需将更简单的公式粘贴到F4:I9中(如下所述)。 But my formula works if one month has 2 values and another month has 12 values. 但是,如果一个月有2个值,而另一个月有12个值,则我的公式有效。

If your original data is in A1:B10 and your transposed heading are in E4:E5 like the screenshot of the spreadsheet shows then you can put this in F4 and copy it to F4:Z10 如果原始数据位于A1:B10中,而转置标题位于E4:E5中(如电子表格的屏幕截图所示),则可以将其放在F4中并将其复制到F4:Z10

=IF(IF(ISERROR(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"")

Gow it works: 天哪,它的工作原理:

The integral bit is OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0) which gets the value from a certain cell based on the value in E4. 整数位是OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0) ,它根据值从某个单元格中获取值在E4中。 (This is what you would paste into F4:I9 if every month had exactly five values.) But that isn't sufficient if the months could each have a different number of values. (如果每个月正好有五个值,这就是您要粘贴到F4:I9中的内容。)但是,如果每个月可以具有不同数量的值,则这还不够。 In that case we need to know how to stop it from getting more values than we want: 在那种情况下,我们需要知道如何阻止它获得比我们想要的更多的值:

If there is another month in E below the current row we can use IF(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1,OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"") to optionally display a value. 如果E在当前行下方还有一个月,我们可以使用IF(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1,OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"")以显示一个值。

If there is no other month in E below the current row we can use IF(MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"") to optionally display a value. 如果当前行下方的E中没有其他月份,我们可以使用IF(MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),"")以显示一个值。

But we need both so we have to use an if to decide which one to use and we end up with the big one. 但是我们两者都需要,所以我们必须使用if来决定要使用哪个,最后我们得到了大个子。 Here it is spread across multiple lines for clarity - you can paste it into the formula bar like this but you can't paste it into a cell directly because of the line feeds. 为了清楚起见,此处它分散在多行中-您可以像这样将其粘贴到公式栏中,但由于换行而不能将其直接粘贴到单元格中。

=IF(
   IF(
      ISERROR(MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),
      MATCH(9.99999999999999E+307,$B:$B)+1>=MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4),
      MATCH($E5,$A:$A,0)>MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-1),
   OFFSET($B$1,MATCH($E4,$A:$A,0)+COLUMN()-COLUMN($E4)-2,0),
   "")

The Nov-2010 would have to be on top of the 1195 in order to use the worksheet's Copy, Paste Special, Transpose command. 为了使用工作表的“复制,选择性粘贴,转置”命令, Nov-2010必须在1195年之上。 The OFFSET function could work with a little row and column maths but that is a volatile¹ function and best kept for when nothing else will suffice. OFFSET函数可以使用一些行和列数学运算,但这是一个易失性¹函数,当没有其他条件可以满足时,最好保留该函数。 The INDEX function provides a non-volatile alternative to OFFSET. INDEX功能提供了OFFSET的非易失性替代方案。

Your sample data neatly offers 5 associated entries in column B for each date in column A. It has been my experience that sample data is not always representative of actual data. 您的样本数据为A列中的每个日期整齐地在B列中提供了5个相关条目。根据我的经验,样本数据并不总是代表实际数据。 You will have to fill every row right to the maximum that any one date could have associated values. 您必须将每一行填满,直到任何一个日期可以具有关联值的最大值为止。

In E4:F4 as standard formulas, 在E4:F4作为标准公式中,

'in E4
=IFERROR(INDEX(A:A,
               AGGREGATE(15, 6, ROW(A$1:INDEX(A:A, MATCH(1E+99,A:A )))/SIGN(LEN(A$1:INDEX(A:A, MATCH(1E+99,A:A )))),
               ROW(1:1))), "")
'in F4
=IFERROR(INDEX(INDEX($B:$B, MATCH($E4,$A:$A, 0)):INDEX($B:$B,
               IFERROR(MATCH($E5,$A:$A, 0)-1, MATCH(1E+99,$B:$B ))),
               COLUMN(A:A)), "")

Fill E4 down until you run out of dates from column A to retrieve. 向下填充E4,直到用完A列中的日期以进行检索。 Fill F4 right until you have reached the maximum of associated values that any date could have then fill down to the extent of the dates in column E. 立即填充F4,直到达到最大关联值为止,然后任何日期都可以填充到E列中的日期范围。

聚合偏移量


¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes. ¹ 每当整个工作簿中的任何内容发生更改时,挥发性函数都会重新计算,而不仅仅是影响其结果的某些更改。 Examples of volatile functions are INDIRECT , OFFSET , TODAY , NOW , RAND and RANDBETWEEN . 易失函数的示例包括INDIRECTOFFSETTODAYNOWRANDRANDBETWEEN Some sub-functions of the CELL and INFO worksheet functions will make them volatile as well. CELLINFO工作表功能的某些子功能也会使它们易失。

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

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