简体   繁体   中英

Add each value in one column to each value in second column in Google Sheets

Would like to have each value in one column added to each value in a second column to created a compounded array. Was thinking I could use ARRAYFORMULA for this, but not that easy, it appears. I want the result column automatically expanded like ARRAYFORMULA does because the number of rows can change and be unequal between Col 1 and Col 2:

Col 1 | Col 2 | Result
------+-------+-------
10    |   1   | 11
------+-------+-------
20    |   2   | 12
------+-------+-------
30    |   3   | 13
------+-------+-------
40    |       | 21
------+-------+-------
50    |       | 22
------+-------+-------
      |       | 23
------+-------+-------
      |       | 31
------+-------+-------
      |       | 32
------+-------+-------
      |       | 33
------+-------+-------
etc...

I could write a script that creates this ONEDIT() when an indicator value changes, but would prefer a formula.

Thanks!

Finally, after goofing around for a while, I figured it out. Perhaps this will help someone else or spur someone on to a more elegant answer than this. In Google Sheets, put this formula in the top cell (C2 in the example table) and the rest of the values will be automatically expanded and added properly:

=ARRAYFORMULA(ARRAYFORMULA(TRANSPOSE(SPLIT(REPT(JOIN(",",$A1:$A10),COUNT($B1:$B10)),",")))+ARRAYFORMULA(TRANSPOSE(ARRAYFORMULA(SPLIT(JOIN(",",(REPT(","&$B1:$B10,COUNT($A1:$A10)))),",")))))

This assumed that each column could have up to 9 rows. Can obviously adjust the number of rows based on the range that you specify for each column. This takes the values in each column, combines them into an array which it repeats and joins/splits or joins and then repeats and splits... to create the correct combination. One formula that automatically expands and is flexible without scripting. Please share a better way to do this, if there is.

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