How do I convert rows to columns, and repeat the adjacent cells? - excel

I'm sorry if the title is confusing, but a visual should hopefully help:
Here's what my sheet looks like:
A B C D
1 x y z t
2 q w e r
3 y u i o
I need to generate a separate sheet wherein:
A B C
1 x y t
2 x z t
3 q w r
4 q e r
...
Basically, I need the middle two columns of the original sheet to be transposed and the adjacent columns to it pulled into my new sheet as well (as duplicate rows).
I have the transpose working correctly, and when I pull the adjacent columns that works too. The issue is, I can't autofill the sheet. When I try to drag & autofill, instead of autofilling using row 2 from the original sheet, the new sheet will autofill using row 3 (which is the same row # in the new sheet).
Please let me know if this isn't making sense, I'll try to explain better! I'm not very well versed in the Google Spreadsheets scripts - I've tried before but they seem rather cumbersome. But I'm happy to try that as well.

I'm not sure where a transpose operation would come into play but a little conditional maths and the INDEX function¹ should suffice.
=INDEX($A$1:$D$3,(ROW(1:1)-1)/2+1,IF(COLUMN(A:A)=3,4,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))))
The documentation I've linked to is Excel but the syntax is identical for these purposes.
Addendum for more columns
By adjusting the condition for the offset after the third column, more columns can be readily accounted for.
=INDEX($A$1:$J$3,(ROW(1:1)-1)/2+1,IF(COLUMN(A:A)>=3,COLUMN(A:A)+1,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))))
The above collects 10 columns from the source matrix but should be auto-adjusting for pretty much any number of columns.
¹ The INDEX function accepts parameters for both row number and column number. Although typically only one of these is used, there is no restriction against supplying both against a 2D range of cells.
Put the row_number calculation,      =(ROW(1:1)-1)/2+1 into a cell and fill down. You will receive 1, 1, 2, 2, 3, 3, etc. This supplies the repeating row number from the A1:D3 range.
Put the column_number calculation,      =IF(COLUMN(A:A)=3,4,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))) into a cell and fill right and down. You will receive 1, 2, 4 for the first row and 1, 3, 4 for the second. This pattern repeats itself for subsequent rows and supplies the offset column numbers from the A1:D3 range.

You can do this with 3 single arrayformulas that are dynamic to work with any number of rows of data:
Here is an image to demonstrate:
The three formulas are:
CELL A1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(Sheet1!A1:A&";",2))),";"))
CELL B1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(Sheet1!B1:B&";"&Sheet1!C1:C)),";"))
CELL C1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(Sheet1!D1:D&";",2))),";"))

Related

how do I swap a cell in a column to another cell in another column for every nth row

This is what I have: This is what I want:
A B C A B C
1 b ab ab ab
2 x x x x
3 c ac ac ac
And there are several hundred rows below. What I need is to replace the content of column A with the content of Column B for every nth row
Not sure how complex your data are, but here's a hacky method as long as "nth" is consistent and you don't mind duplicating your data into another sheet or columns.
For example, let's say you want to replace the value for every 4th row, starting with the first row of data.
Add a column to your original data (in this case, I'm calling it "whatever"), then start numbering your rows as 4, 5, 6, etc.
Create a column for your new "A-value", and insert a formula such as this, which is checking if the value in the "whatever" column is divisible by 4, and if so, then replace a-value with b-value.
=IF(MOD(A3,4),B3,C3)
Then determine how you want to put together your final data. Copy/paste as special values into another sheet, or whatever works best for you.

All combinations of 4 out of 7 columns with totals using excel

I have 7 columns to choose from and I need to pick 4 of those columns and generate a total for each row. I also need every combination of 4, which means I'll have 35 new columns with the totals for each of those combinations showing in each row. I need the code for this and if it can be done only using Excel. Here is an image of the columns and the grayed ones are the 7 columns I'm talking about. My knowledge of Excel is very limited. There are over 1,500 rows if that matters.
multi step approach that is going to use some helper rows. there may be a more elegant formula that will do this, and much slicker options in VBA, but this is a formula only approach.
Step 1 - Generate List of Column Combination
To generate the list 4 helper rows will need to be insert at the top of your data. either above or below you header row. These 4 rows will represent the column number you are going to pick. To keep the math simpler for me I just assumed the 1 for the first column and 7 for the last column. those numbers will get converted to later to account for column in between in your spreadsheet. For the sake of this example The first combination sum will occur in column AO and the first helper row will be row 1. The first combination will be hard coded and it will seed the pattern for the remainder of column combinations. Enter the following values in the corresponding cells:
AO1 = 1
AO2 = 2
AO3 = 3
AO4 = 4
In the adjacent column a formula will be placed and copied to the right. It will automatically augment the bottom value by 1 until it hits its maximum value at which point the value in the row above will increase by 1 and the the value of the current will be 1 more than the cell above. This will produce a pattern that covers all 35 combinations by the time column BW is reached. Place the formulas below in the appropriate cell and copy to the right:
AP1
=IF(AO2=5,AO1+1,AO1)
AP2
=IF(AO2=5,AP1+1,IF(AO3=6,AO2+1,AO2))
AP3
=IF(AO3=6,AP2+1,IF(AO4=7,AO3+1,AO3))
AP4
=IF(AO4=7,AP3+1,AO4+1)
Step2 - Sum The Appropriate Columns
I was hoping to use a some sort of array type operation to read through the column reference numbers above, but I could not get my head around it. Since it was just 4 entries to worry about I simply added each reference manually in a SUM function. Now the important thing to note is that we will be using the INDEX function over the 13 columns that cover the range of your columns so to convert the index number we figured out above, to something that will work to grab every second row, the number that was calculated will be multiplied by 2 and then 1 will be subtracted. That means 1,2,3,4 for the first column combination becomes 1,3,5,7. You can see this in the following formula. Place the following formula in the appropriate cell and copy down and to the right as needed.
AO5
=INDEX($AB5:$AN5,AO$1*2-1)+INDEX($AB5:$AN5,AO$2*2-1)+INDEX($AB5:$AN5,AO$3*2-1)+INDEX($AB5:$AN5,AO$4*2-1)
pay careful attention to the $ which will lock row or column reference and prevent them from changing as the formula is copied.
Now you may need to adjust the cell references to match your sheet.

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))

Any ideas on how to turn certain cells absolute?

Here is my spread sheet:
What I'm trying to do is break down my total weight into individual weights, to do this I have to make total weight absolute so I can multiply my % ratios. Only problem is some skus have 6 items some have 5 items so I can't just set every X rows make absolute. essentially I want to do this.
P.S. I have about 5000 rows so it'd take a really long time to manually do it. Wondering if there's any solutions thanks.
You could use nested if functions, it's dirty but it works:
The function goes in J column
Check if the associate H column is empty
If it is empty return empty to keep the spreadsheet clean
If it is not empty check the row above in I column for empty
If it is not empty that the total weight to multiply the ratio by the total weight
If it is empty check the row above that
And repeat as necessary.
I wrote this function that checks 5 deep I think? You could keep nesting it to check 10 deep, 20 deep, etc., until you reach the nested IF limit in excel which I think is 255 so that it adapts to your varying needs in the items list.
=IF(H7<>"",IF(I6<>"",H7*I6,IF(I5<>"",H7*I5,IF(I4<>"",H7*I4,IF(I3<>"",H7*I3,IF(I2<>"",H7*I2))))), "")
Not sure if you just want the J column filled based on the H column, but here's my suggestion. I tend to use hidden columns as they keep my formulae simple but you can incorporate the formula in my G column (the column I'ld hide) into my formula in the F column.
Results Shown
Formulae Shown
P.S. I need reputation as I need to comment on another post.
Sryn
You could use a hidden column with nested If statements, formula goes in Column K:
Check if Column F is blank
If it is not check if the row above is blank
Repeat
Once the row is blank you know you are on the row with the total weight
Set the Column K cell to that value (the I column)
For column J:
If Column K is 0 then return empty
If not multiply Column H by Column K
The formula for Column K is:
=IF(F22 <> "", IF(F21 <> "", IF(F20 <> "", "", I20), I21)) - just add the required amount of nesting, for your case 6 nests is enough
The formula for Column J is:
=IF(K22 = 0, "", K22 * H22)
And then just right click column K and choose to hide that column. Of if you use column K in another part of your sheet just select any unused column.

Excel - Formula or Macro to fill a cell based on another cell that links to yet another cell

In Excel, I am trying to make a cell based of the values contained in two other cells.
I need Cells X and Y to have data based on Cells L and #, like so....
X Y L 1 2 3 4 5 6
A 6 1 1 6;1 6;1 7;1 7;2 7;2 8;1
B 7 2 4 6;1 6;1 7;1 7;2 7;2 8;1
So row A, has columns X and Y filled based of the values in the number columns. The specific number needed is what is filled in in column L.
I am not sure the best way to phrase this question. If my example doesn't make sense, I can try to clarify or provide more examples.
I have no idea if this can be done with fancy formulas or with a VBA macro or two. I am an excel noob.
If I've understood your question correctly you can do this with a combination of Left/Right, Index and search.
In my example images, the user inputs their value in column D, and then columns B and C use the formulea
=LEFT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
=RIGHT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
respectively
Here, the Index function returns the correct column to look at (i.e. the value chosen by the user, the Search function finds the position of the semi-colon, and the left/right functions return the values either side of the semi-colon.

Resources