简体   繁体   English

在Excel中将5列合并为一列

[英]Combine 5 columns into one in Excel

I want to merge 5 columns into a single one like this: 我想将5列合并为一个这样的列:

1  4  7  10 13
2  5  8  11 14
3  6  9  12 15

to this 对此

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

I have this formula and I'm trying to adapt it, but it fails :( 我有这个公式,我正在尝试适应它,但是它失败了:(

=IF(ROW()<=COUNTA(A:A),INDEX(A:A,ROW()),IF(ROW()<=COUNTA(A:B),INDEX(B:B,ROW()-COUNTA(A:A)),IF(ROW()>COUNTA(A:C),"",INDEX(C:C,ROW()-COUNTA(A:B))))) = IF(ROW()<= COUNTA(A:A),INDEX(A:A,ROW()),IF(ROW()<= COUNTA(A:B),INDEX(B:B,ROW() - COUNTA(A:A)),IF(ROW()> COUNTA(A:C), “”,INDEX(C:C,ROW() - COUNTA(A:B)))))

The statement uses 3 IF functions, because it needs to combine 3 columns: 该语句使用3个IF函数,因为它需要合并3列:

  1. For column A, the function compares the row number of a cell with the total number of cells in A column that are not empty. 对于A列,该函数将一个单元格的行号与A列中不为空的单元格总数进行比较。 If the result is true, the function returns the value of the cell from column A that is at row(). 如果结果为true,则该函数从row()处的A列返回单元格的值。 If the result is false, the function moves on to the next IF statement. 如果结果为假,该函数将继续执行下一个IF语句。
  2. For column B, the function compares the row number of a cell with the total number of cells in A:B range that are not empty. 对于B列,该函数将一个单元格的行号与A:B范围内不为空的单元格总数进行比较。 If the result is true, the function returns the value of the first cell that is not empty in column B. If false, the function moves on to the next IF statement. 如果结果为true,则函数返回列B中第一个不为空的单元格的值。如果为false,则函数移至下一个IF语句。
  3. For column C, the function compares the row number of a cell with the total number of cells in A:C range that are not empty. 对于C列,该函数将一个单元格的行号与A:C范围内不为空的单元格总数进行比较。 If the result is true, the function returns a blank cell and doesn't do any more calculation. 如果结果为true,则该函数将返回一个空白单元格,并且不再进行任何计算。 If false, the function returns the value of the first cell that is not empty in column C. 如果为false,则该函数返回列C中第一个不为空的单元格的值。

Do you have any ideas? 你有什么想法?

You can use the following formula, so long as your results are not in the same row or column as the original data (and could be on another worksheet). 只要您的结果与原始数据不在同一行或列中(并且可以在另一工作表中),就可以使用以下公式。 In addition, the formula would fail if there are empty cells in the first row or first column. 此外,如果第一行或第一列中有空单元格,则公式将失败。

=IFERROR(INDEX($A$1:$E$3,MOD(ROWS($1:1)-1,COUNTA($A:$A))+1,INT((ROWS($1:1)-1)/COUNTA($A:$A))+1),"")

The row argument and the column argument for the INDEX function, if you tease it apart, you will see that they return the proper sequence of arguments. 如果将INDEX函数的row参数和column参数分开,它们将返回正确的参数序列。 However, you can add any number of columns or rows to the array. 但是,您可以将任意数量的列或行添加到数组。

If you want to define the array as being arbitrarily large, so if your real array can expand without having to change the formula, then, on another worksheet, (eg Sheet2), where myArtray is, for example, Sheet1!$A$1:$Z$100 , try: 如果要将数组定义为任意大,那么如果您的实际数组可以扩展而不必更改公式,则在另一个工作表(例如Sheet2)上, myArtray例如为Sheet1!$A$1:$Z$100 ,请尝试:

=IFERROR(IF(INDEX(myArray,MOD(ROWS($1:1)-1,COUNTA(Sheet1!$A:$A))+1,INT((ROWS($1:1)-1)/COUNTA(Sheet1!$A:$A))+1)="","",
INDEX(myArray,MOD(ROWS($1:1)-1,COUNTA(Sheet1!$A:$A))+1,INT((ROWS($1:1)-1)/COUNTA(Sheet1!$A:$A))+1)),"")

I have tested your formula and it works as expected, it may be though that you are placing your formula within one of the columns that you reference in your formula which will create a circular reference. 我已经测试了您的公式,它可以按预期工作,尽管您可能将公式放在公式中引用的其中一列中,这将创建一个循环引用。

In other words, try using your formula in a column that isn't A, B or C and see what happens. 换句话说,请尝试在非A,B或C的列中使用公式,然后看看会发生什么。

Side note: For what you are trying to achieve I would recommend trying to produce a VBA macro instead, your formula could get quite long and fiddly as you add more and more columns where as a simple macro would let you do something like this with any number of columns. 旁注:对于您要实现的目标,我建议尝试改为生成VBA宏,因为您添加了越来越多的列,因此公式可能会变得很冗长而烦躁,因为一个简单的宏可以让您对任何列数。

You can paste this in a code module 您可以将其粘贴到代码模块中

Public Function OneDimensionalRange(source As Range)
    Dim index As Integer
    index = Application.Caller.Row
    OneDimensionalRange = source.Cells(index).value
End Function

It'll be available for use as a worksheet function. 它可以用作工作表功能。

=OneDimensionalRange($A$3:$E$3) = OneDimensionalRange($ A $ 3:$ E $ 3)

这是我需要的直到S的要求公式。

=IF(ROW()<=COUNTA(A:A),INDEX(A:A,ROW()),IF(ROW()<=COUNTA(A:B),INDEX(B:B,ROW()-COUNTA(A:A)),IF(ROW()<=COUNTA(A:C),INDEX(C:C,ROW()-COUNTA(A:B)),IF(ROW()<=COUNTA(A:D),INDEX(D:D,ROW()-COUNTA(A:C)),IF(ROW()<=COUNTA(A:E),INDEX(E:E,ROW()-COUNTA(A:D)),IF(ROW()<=COUNTA(A:F),INDEX(F:F,ROW()-COUNTA(A:E)),IF(ROW()<=COUNTA(A:G),INDEX(G:G,ROW()-COUNTA(A:F)),IF(ROW()<=COUNTA(A:H),INDEX(H:H,ROW()-COUNTA(A:G)),IF(ROW()<=COUNTA(A:I),INDEX(I:I,ROW()-COUNTA(A:H)),IF(ROW()<=COUNTA(A:J),INDEX(J:J,ROW()-COUNTA(A:I)),IF(ROW()<=COUNTA(A:K),INDEX(K:K,ROW()-COUNTA(A:J)),IF(ROW()<=COUNTA(A:L),INDEX(L:L,ROW()-COUNTA(A:K)),IF(ROW()<=COUNTA(A:M),INDEX(M:M,ROW()-COUNTA(A:L)),IF(ROW()<=COUNTA(A:N),INDEX(N:N,ROW()-COUNTA(A:M)),IF(ROW()<=COUNTA(A:O),INDEX(O:O,ROW()-COUNTA(A:N)),IF(ROW()<=COUNTA(A:P),INDEX(P:P,ROW()-COUNTA(A:O)),IF(ROW()<=COUNTA(A:Q),INDEX(Q:Q,ROW()-COUNTA(A:P)),IF(ROW()<=COUNTA(A:R),INDEX(R:R,ROW()-COUNTA(A:Q)),IF(ROW()>COUNTA(A:S),"",INDEX(S:S,ROW()-COUNTA(A:R)))))))))))))))))))))

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

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