looping through columns to adjust formula - excel

i have 3 rows of data and 17 columns, in row 2 depending on a selection (low, med, high, which is cell referenced as say 1, 2, 3) i want to have formula that if low is selected row 2 would equal corresponding column in row 1 and add 1.
effectively some code i was thinking of is:
For i = 2 to 17
Cells(2,i).Formula = "Cells(1,i) + 1"
I need the output to actually be a formula and not a value though

Related

Look up for highest value from another column if values are equal excel

***1 2 3***
a 2 3
b 3 4
c 4 3
d 5 2
so I know to get the highest value I do
=INDEX(column1, MATCH(MAX(column3), column3, 0))
... which would give me 'b'
now I want to get the second highest value based on the column 3 but because there are two cells with 3 (which is the second highest value) I want to use the one that has the lowest value in column 2 based on those two rows. Is this possible?
Use a 'helper' column that adds column C + (column B ÷ 10) and use a modification of your original formula on that column.
        
The standard formula in F5 is,
=INDEX(A$2:A$5, MATCH(AGGREGATE(14, 6, D$2:D$5, ROW(1:1)), D$2:D$5, 0))
Fill down as necessary.

repeating a formula conditionally in excel 2013?

I am trying to calculate a formula and then dragging it to apply it for the whole column but the problem is that I want to compare first cell of the column A with the first cell of the column B and then second cell of the column A with the second cell of the column B and then compare third cell of the column a with the first cell of the column B... :
A B result
1 1 4 0
2 2 5 0
3 3 0
4 4 0
5 5 1
6 5 1
when i write the pattern like if =IF((A1<Sheet1!B1),0,1) then =IF((A2<Sheet1!B2),0,1) ,=IF((A1<Sheet1!B1),0,1),=IF((A2<Sheet1!B2),0,1)
Four times and then dragging the formula for the column it start comparing it with the correspond one =IF((A5<Sheet1!B5),0,1) How should i change it ?
edit :
in the example I want to compare cell(1,A) with cell(1,B) then cell (2,A) with cell (2,B) and then cell(3,A) with cell(1,B) then cell (4,A) with cell (2,B) and then cell(5,A) with cell(1,B) then cell (6,A) with cell (2,B).[repeating the pattern two times and then start over]
Try this in C1,
=--NOT(A1<OFFSET('Different Sheet'!$B$1, MOD(ROW(1:1)-1, 2), 0))
Fill down as necessary. You only seem to be looking for a 0 or a 1 so I've simplified your IF statement. (Note: maths with MOD adjusted for more universality)
For different multiples you should only have to change the divisor parameter of the MOD function. The ROW function used as ROW(1:1) will return 1, 2, 3, 4, 5, etc as you fill down. MOD returns the remainder of a division operation so MOD(ROW(1:1)-1, 3) filled down will return 0, 1, 2, 0, 1, 2, 0, etc.
If you used the COUNT function on the numbers in 'Different Sheet'!B:B, you should be able to achieve a dynamic divisor.
=--NOT(A1<OFFSET('Different Sheet'!$B$1, MOD(ROW(1:1)-1, COUNT('Different Sheet'!B:B)), 0))

Dividing a column into N equal groups by value

Say I have a column with values:
23
24
25
66
67
84
81
85
I want to divide this into N groups, say N right now is 4.
23,1
24,1
25,2
66,2
67,3
84,3
81,4
85,4
I actually need to divide around 30k sorted values into groups 1 to 99; each with equal number of elements.
Any quick way to do this in Excel?
With data in column A, in B1 enter:
=A1 & "," & ROUNDUP(ROW()/(COUNT(A:A)/4),0)
and copy down. For example:
.
Change the 4 in the formula to vary the number of groups.
I use this trick for equal data bucketing. Suppose you have data in A1:A8 range. Put this formula in B1:
=MAX( ROUNDUP( PERCENTRANK($A$1:$A$8, A1) *4, 0),1)
Fill down the formula all across B column and you are done. The formula divides the range into 4 equal buckets and it returns the bucket number which the cell A1 falls into. The first bucket contains the lowest 25% of values.
Adjust the number of buckets according to thy wish:
=MAX(ROUNDUP(PERCENTRANK([Range],[OneCellOfTheRange]) *[NumberOfBuckets],0),1)
The number of observation in each bucket will be equal or almost equal. For example if you have a 100 observations and you want to split it into 3 buckets then the buckets will contain 33, 33, 34 observations. So almost equal. You do not have to worry about that - the formula works that out for you.
if this is in column A
row 1
row 2
row 3
row 4
row 5
place formula in column B
=MOD(ROW(); 4)+1
this result in
row 1, 2
row 2, 3
row 3, 4
row 4, 1
row 2, 2

Convert a table to text in excel - each cell on a new line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table in excel, in the example below each number represents a cell:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
I would like to convert this table to a text file that looks like this:
11 12
13
14
15
21 22
23
24
25
31 32
33
34
35
I am aware of the transpose function and a table to text but I can't seem to achieve what I illustrated above.
Using a simple VBA is efficient to create the actual file.
This code dumps data from the range from A1 to the last used cell in column E of the activesheet, to a file C:\temp\dummy.txt
Please change your path to suit
Sub RipData()
Dim X
Dim lngRow As Long
Dim lngCol As Long
Dim objFSO As Object
Dim objTF As Object
X = Range([a1], Cells(Rows.Count, "E").End(xlUp))
Set objFSO = CreateObject("scripting.filesystemobject")
Set objTF = objFSO.createtextfile("C:\temp\dummy.txt")
For lngRow = 1 To UBound(X, 1)
objTF.writeline X(lngRow, 1) & vbTab & X(lngRow, 2)
For lngCol = 3 To UBound(X, 2)
objTF.writeline X(lngRow, lngCol)
Next
objTF.writeline
Next
objTF.Close
End Sub
I usually apply a combination of ROUND, MOD, and OFFSET to turn a tabular set of data into a single column. This problem has the added wrinkle of wanting to put the second number in a group in a column to the right while skipping that number in the first column. And you want to put place a spacer row between each group of numbers. Both of these requirements make for more complicated than usual formulas.
For the first column, beginning in cell A5, or in another cell in column A the row of which is an even multiple of 5, use the following formula,
= IFERROR(
OFFSET(
$A$1,
ROUNDDOWN( ROW(A5) / 5 - 1, 0),
MOD( ROW(A5), 5) + (MOD( ROW(A5), 5) <> 0)
) /
(MOD( ROW(A5), 5) <> 4),
""
)
and copying down the column. This assumes that the data rows begin in cell A1.
For the second column, beginning with the cell in column B to the right of the starting cell in column A, enter this formula,
= IFERROR(
OFFSET($A$1,
ROUNDDOWN( ROW(B5) / 5 - 1, 0),
MOD( ROW(B5), 5) + 1
) /
NOT( MOD( ROW(B5), 5) > 0),
""
)
again copying it down.
How the formulas work
Both the column A and the column B function are elaborations of OFFSET, which takes as its arguments a starting address, the number of rows down (or up) its result range will begin, and the number of columns to the right (or left) that the range will. (It also takes two more arguments, which are the width and the height of the range to return. Since we're only concerned with single cells, we can leave these two arguments out.)
For example, the row calculation for column A uses the expression
ROUNDDOWN(ROW(A5)/5-1,0).
In cell A5, this formula resolves to (5/5 - 1) or 0, with no rounding needed. So, the row offset is 0 from A1, which makes sense because the A5 formula is processing the first row of data.
In cell A6, the formula becomes 6/5 - 1, or 1.2 - 1, or 0.2, which rounds down to 0, again what we need since we're still grabbing numbers from row 1.
This continues until cell A10, when we get 10/5 - 1, or 1. We're finished with the first row of data (which had an offset of 0 rows from a1) and now move on to the second. The value of the row offset will continue as 1 up to cell A15, when it will go up by 1 again.
The calculation of the column offset is a bit trickier:
MOD(ROW(A5),5)+(MOD(ROW(A5),5)<>0))
The first term is MOD(ROW(A5),5). In cell A5, that becomes MOD(5, 5), with a result of 0 since the integer remainder of 5 / 5 is 0. Again, makes sense--the column offset of 0 from cell A1 means the we will pick up the value in column A. In cell A6, we've got MOD(6, 5), for a column offset of 1. This means the value in A6 will come from column B.
But we don't want that: the value in column B of each data row is supposed to be shown in column B of the result range. We need to skip from column A to column C to get the next value for column A of the results.
Hence the second term (MOD(ROW(A5),5)<>0)). This evaluates to TRUE for every row that is not an even multiple of 5--these are the rows that show a (result) value in column A and column B. TRUE evaluates to 1 when used in an arithmetic expression. So, we are adding 1 to the column offset when the formula is in rows 6, 7, 8, 9, 11, 12, etc., thereby skipping over column B of the data row.
Finally, the divisor (MOD(ROW(A5),5)<>4). This expression will evaluate to TRUE (or 1) when the row the formula is in does not have a remainder of 4 when divided by 5. That means it evaluates to FALSE or 0 only when the formula is in rows 9, 14, 19, etc.
These in fact are the rows for which we want a space between the groups of numbers. The purpose of this divisor is to produce a formula error when the formula is in those rows. We then catch that error in the IFERROR function that encloses the entire formula, and the output of the formula becomes "" -- the empty string.
I won't go through the column B expression, which uses the same kind of maneuvers to pick up the second value in each data row and show it beside the first values.
For convenience in copying and pasting to your worksheet, here are the unformatted versions of the formulas:
Cell A5 Formula: =IFERROR(OFFSET($A$1,ROUNDDOWN(ROW(A5)/5-1,0),MOD(ROW(A5),5)+(MOD(ROW(A5),5)<>0))/(MOD(ROW(A5),5)<>4),"")
Cell B5 Formula: =IFERROR(OFFSET($A$1,ROUNDDOWN(ROW(B5)/5-1,0),MOD(ROW(B5),5)+1)/NOT(MOD(ROW(B5),5)>0),"")

Multiply row with above row and obtain an average

I have an excel table with lets say 500 columns and 2 rows. I want to multiply each cell in the second row with the cell above, in the first row. Then get the average of the sum.
Example:
3 4 1 2 5
1 3 3 5 1
Solution would be: (3*1 + 4*3 + 1*3 + 2*5 + 5*1) / number of columns
What would the command look like in Excel?
For data in rows 1 and 2 use
=SUMPRODUCT(1:1,2:2)/COUNT(1:1)
Here's what I'd do:
1) In the third row first column enter "=A1+A2". Copy/paste that across the entire row to propagate.
2) In another column somewhere enter "AVERAGE(C1:C500)".
That cell in #2 should give you the answer.

Resources