简体   繁体   中英

Excel: Merge two columns into one column with alternating values

how can I merge two columns of data into one like the following:

Col1    Col2    Col3
========================
A       1       A
B       2       1
C       3       B
                2
                C
                3

You can use the following formula in column D as per my example. Keep in mind to increase the $A$1:$B$6 range according to your data.

=INDEX($A$1:$B$6,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1)

Result:

在此处输入图片说明

Thank you to @Koby Douek for the answer. Just an addition--if you are using Open Office Calc, you replace the commas with semi-colons.

=INDEX($A$1:$B$6;INT((ROWS(D$2:D2)-1)/2)+1;MOD(ROWS(D$2:D2)-1;2)+1)

Expanding @koby Douek's answer to more columns and explaining some of the terms

Original Code for 2 columns to 1 alternating =INDEX($A$1:$B$6,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1)

$A$1:$B$6 Defines the columns and rows to source the final set of data from, the $s are only present to keep the formula from changing the columns and rows selects if it is copied and pasted or dragged.

To extend to work on any values you dump into the columns instead of having to expand the range every time it should be amended to $A:$B or A:B so you can easily copy it to other sets of columns and create new merges, but it will also give the 1st value in every column as one of the alternating values so if you instead have headers you would be able to do this by instead using a large number so $A$1:$B$99999 or A$1:B$99999 if you want to past and move the columns ymmv which is better by situation.

lets assume you are fine including the values in the 1st row

This changes the formula to =INDEX($A:$B,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1)

Now on to D$2:D2

This is the row that is being used to calculate the difference between the current row the formula is in ( D2 ) and the reference row ( D$2 ) The important thing to make sure you do is to set the reference row number to the 1st row you will be putting values in, so if your 1st row is a header in the sort column you will use the 2nd row as the reference, if your values in the combined column D begin on the 3rd row then the reference row would be D$3

Since I like the more general form where the 1st row isn't a header row I'll use D$1:D1 but you could still mix source rows without headers into a combined row with a header of as many rows as you like just by incrementing that reference row number to be the 1st row where your values should begin.

This changes the formula to =INDEX($A:$B,INT((ROWS(D$1:D1)-1)/2)+1,MOD(ROWS(D$1:D1)-1,2)+1)

Now INT((ROWS(D$1:D1)-1)/2)+1 and MOD(ROWS(D$1:D1)-1,2)+1

INT returns an integer value so any decimal places are dropped, it essentially functions like rounding down to the nearest whole number

MOD functions by returning the remainder of a division, it's result will be a whole number between 0 and n-1 where n is the number we are dividing by. (eg: 0/3=0; 1/3=1; 2/3=2; 3/3=0; 4/3=1 ... etc)

So -1)/2)+1 and -1,2)+1

the first value is again the difference between the current row and the reference row. but D$1:D1 is going to be the count of the rows, which is 1 so we have to correct for the rows count starting at 1 instead of 0 which would throw off our calculations, so both are using the -1 to reduce the count of the rows by 1

in the case of /2 and ,2 both are because we are dividing by 2 in the first statement it's a normal division by 2 /2 in the modulus statement it's an argument of the Mod function so ,2

finally we need to add 1 using +1 to correct for the index's need to have a value series which begins at 1.

INT((ROWS(D$2:D2)-1)/2)+1 is finding the row number to select the value from.

MOD(ROWS(D$1:D1)-1,2)+1 is finding the column number to select the value from

Thus we can change /2 and ,2 to /3 and ,3 to do this with 3 columns

This yields: =INDEX($A:$B,INT((ROWS(D$1:D1)-1)/3)+1,MOD(ROWS(D$1:D1)-1,3)+1)

So maybe that's the confusing way to look at it but it's closer to how my mind works on it. Here is an alternative view:

=INDEX([RANGE],[ROW_#],[COLUMN_#]) returns the value from a range of rows and columns

Using the example: =INDEX($A:$B,INT((ROWS(D$1:D1)-1)/3)+1,MOD(ROWS(D$1:D1)-1,3)+1)

[RANGE] = $A:$B this is the range of source columns.

[ROW_#] = INT((ROWS(D$1:D1)-1)/3)+1

INT([VALUE_A])+1 returns an integer value so any decimal places are dropped. Then adds one to it. we add one to the value because the result of the next steps will be 1 less than the value we need.

[Value_A] = (ROWS(D$1:D1)-1)/3 ROWS(D$1:D1) returns the number of rows in the Range to the current row in the results column, we use D$1 to designate the row number where the values in the results column begin. D1 is the current row in the results column giving us a range from the source row, allowing us to count the rows. we have to subtract 1 from this value using -1 to get the difference between the source and current. This is then divided by /3 because we have three columns we want to look through in this example so we only change rows when the result is divisible by 3. the INT drops any decimal places as mentioned so it only increments when cleanly divisible by 3.

[COLUMN_#] = MOD(ROWS(D$1:D1)-1,3)+1

MOD([VALUE],[Divisor])+1 returns the remainder of the value when divided by the divisor.

Using the example: MOD(ROWS(D$1:D1)-1,3)+1

In this case we still divide by 3 but it's an argument to the MOD function, we still need to count the number of rows and subtract 1 before dividing it, this will return a 0, 1, or 2 for the column, but as above we are shifted backwards by 1 as the column numbers begin with the number 1, so as before we must add 1

And here we add column A and D two different formulas depending on if you add the formula to an odd row or an even row.

https://1drv.ms/x/s?AncAhUkdErOkguUaToQkVkl5Qw-l_g?e=5d9gVM

Odd Start row

=INDEX($A$2:$D$9;ROUND(ROW(A1)/2;0);IF(MOD(ROW()-ROW($A$2);2)=1;4;1)) 

Even Start row

=INDEX($A$2:$D$9;ROUND(ROW(A1)/2;0);IF(MOD(ROW()-ROW($A$1);2)=1;4;1))

What is A1 in the picture is the cell directly above your first data cell.

在此处输入图像描述

If you want to place it on a different sheet you just add the sheet name:

=INDEX(MySheet!$A$2:$D$9;ROUND(ROW(MySheet!A1)/2;0);IF(MOD(ROW()-ROW(MySheet!$A$2);2)=1;4;1))
=INDEX(MySheet!$A$2:$D$9;ROUND(ROW(MySheet!A1)/2;0);IF(MOD(ROW()-ROW(MySheet!$A$1);2)=1;4;1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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