A simple voting mechanism in excel - excel

I have an excel sheet which has three columns. These three columns can have some value or be blank. I have to update a fourth column based on the entries in these three columns. The criteria for updating the fourth column is as follows:
If two or more columns have same value then fourth column will have
this same value.
If all three values are different then fourth column will have first
non blank value.
If there is only one non blank value then fourth column will have
that non blank value.
I'm unable to figure out what should be the formula in fourth column for this criteria.
Here is an example

=IFERROR(INDEX(A2:C2,,MODE(MATCH(A2:C2,A2:C2, 0))), IF(A2="", IF(B2="", C2, B2), A2))
Just paste it to D2 and drag down.
To explain it, it looks for the MODE (Most common value) of everything in the range A2:C2.
MODE(MATCH(A2:C2, A2:C2, 0))
This would return the first position in which the most common value returns, in this case we use the INDEX function and get the value at the given index, in the range A2:C2.
INDEX(A2:C2,,MODE(MATCH(A2:C2,A2:C2, 0)))
This part so far is slightly based on code from here
Now this will give us the letter, but only if one letter is more common than the others, if they all have different values, then it'll return an error. In this case, we use the IFERROR function which, if there is an error in it's first argument, will instead calculate the second argument. So if there isn't a match, then we want to just get the first non blank cell.
So check if A2 is blank, if it is check if B2 is blank, if it is, use the cell value in C2, otherwise use the cell value in B2, but if A2 is not blank, use the value in A2.
IF(A2="", IF(B2="", C2, B2), A2))
With a little bit of working around, you could probably turn the above statement into something that's a little more expandable (google first non-blank value in row or something like that).
Hope this helps.

Using your example, in cell D2 and copied down:
=IF(COUNTA(A2:C2)=0,"",IF(MAX(INDEX(COUNTIF(A2:C2,A2:C2),))>1,INDEX(A2:C2,MATCH(MAX(INDEX(COUNTIF(A2:C2,A2:C2),)),INDEX(COUNTIF(A2:C2,A2:C2),),0)),INDEX(A2:C2,MATCH(TRUE,INDEX(A2:C2<>"",),0))))

Related

Excel - Offset to last non-blank cell

From my research, when a bunch of cells are merged, you can only reference the first row and first column of the merged cells. EG. if A1:A3 are merged, then I can only access the data using A1 only, and A2 and A3 returns 0.
Now let's say I have a column B that has a formula that calculates based on values in column A. If I drag this formula down, then B2 and B3 will end up using value of 0, when they should be using value in A1.
Effectively, what i want to do is "if the cell in column A (of this row) is blank, then use the last non-blank value going upwards".
I know this will need to combine a couple of formulas, but I can't figure out how to create this. For a start, I can use the Offset function to "go up", but the difficult part here is how to find the previous non-blank cell?
I also tried combing OFFSET with COUNTA (see https://www.exceltip.com/other-qa-formulas/get-the-value-of-the-last-non-blank-cell-in-a-column-in-microsoft-excel.html), but this doesn't work if this occurs multiple times.
Easiest way is to use a helper column:
In B2 write
=IF(NOT(ISBLANK(A2)),0,B1+1)
and in C2 write
=OFFSET(A2,-B2,0)
Edit: actually... the solution without helper column is even easier! Write in B2:
=IF(ISBLANK(A2),B1,A2)
To avoid the helper column, you can use the INDEX + AGGREGATE functions:
=INDEX($A$1:A1,AGGREGATE(14,6,($A$1:A1<>"")*ROW($A$1:A1),1))

Check if text exist in rest of Column, and if so, use info from that row it is in

I'm after a way to a cell to check another cell that I'm inputting text into, and for it to see if that text value is the same anywhere else in the column, and if so, it grabs the number value which is in the same column as itself but in the row of the text that checked for.
So if you use picture, you can see I've currently got E7 selected. I'm wanting it to check the "GOLF COURSE" column for any other row that contains the same text it has in it's own row. For this it's "Course1". I'd like it to check through the rest of column B if there are any matches for "Course1" which there is in B3. If it matches I'm wanting it to then use the value that's in same column as it (E) but the same row as the matched text in column B. In this case I would want it to copy the value that is in E3.
If there wasn't a match (as it's a new course lets say) then I need to be able to just click on the cell and input the numbers needed, which I would be able to do anyway but just throwing it in for sake of info.
I've tried all sorts of googling and thinking how I could possibly do it but it's too difficult for my amateur knowledge of Excel.
I believe you are looking for INDEX/MATCH:
=IF(COUNTIF($B:$B,$B7)>1,INDEX(E:E,MATCH($B7,$B:$B,0)),"New")
I added a COUNTIF check to ensure that the same course exists more than once in column B, without it, you would be getting a circular reference formula (which would also happen with the above formula if the same course appears more than once, but you use this formula on the first occurrence of that course, so make sure do not use it the first time you fill out the PAR scores for a particular course).
Merged Cells Messing With INDEX/MATCH
The Formula
Copy the following formula into cell E7:
=IF(AND($B7<>"",$D7="Par"),IF(ISERROR(MATCH($B7,$B$3:$B5,0)),"Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))),IF(AND($B6<>"",$D7="Strokes"),IF(ISERROR(MATCH($B6,$B$3:$B4,0)),"Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1)),""))
Image
How
We are creating the formula in cell E7.
MATCH
Syntax: MATCH(lookup_value,lookup_array,match_type)
We will use MATCH to find the position of COURSE1 in the above
cells of column B.
The 1st argument is lookup_value which will be the cell in the same
row of our row (7) in column B: B7 where we will lock only the
column (we will not be searching in other columns): $B7.
The 2nd argument lookup_array will be a range. The first cell will be
cell B3 whose row and column we'll lock, because we will always
start the search from this cell in every other cell to the left or
below: $B$3. The last cell will be B5 where we will lock only the
column: $B5.
And finally we will use 0 as the parameter of the 3rd argument
match_type to find an exact match.
Now were ready to write our MATCH formula:
=MATCH($B7,$B$3:$B5,0)
Which will return 1 i.e. an exact (0) match of B7 was found
in the 1st cell of range B3:B5.
We don't want 1 (E3), but the value in the cell (5).
INDEX
The INDEX function has 2 syntaxes where we will use the 2nd:
Syntax 2: INDEX(reference,row_num,column_num,area_num)
Since were using a one-column range we can safely omit the arguments row_num and column_num, which leaves us with:
Modified Syntax: INDEX(reference,area_num)
The INDEX function used this way will return the area_num-th value
of reference i.e. in our case if area_num is 1 it will return the
1st value in our column range, if it is 2, then the 2nd, etc.
The 1st argument reference will be the same sized range of our
MATCH range in column E: $E$3:$E5 where we will remove the
column locks because we also want to return results for other
columns: E$3:E5.
The 2nd argument area_num will be our MATCH formula.
Our current formula looks like this:
=INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))
which will return the value of cell E3: 5.
Final Adjustments: IF, AND and ISERROR
That would have been almost (Error Checking) all if the cells in column B weren't merged. Therefore we have to use IF to determine if the row in which we're writing the formula contains either Par or Strokes and adjust our so far created formula for both conditions:
=IF($D7="Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))
=IF($D7="Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1)
=IF($D7="Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0)),$D7="Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1))
and (3rd condition) check in column B if there is a value in the row where we're creating the formula for a row containing Par, or the row above for a row containing Strokes, using AND:
=IF(AND($B7<>"",$D7="Par"),INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0)),IF(AND($B6<>"",$D7="Strokes"),INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1),""))
Finally we have to add some error checking, because if the match was not found the formula will produce and #N/A error:
=IF(AND($B7<>"",$D7="Par"),IF(ISERROR(MATCH($B7,$B$3:$B5,0)),"Par",INDEX(E$3:E5,MATCH($B7,$B$3:$B5,0))),IF(AND($B6<>"",$D7="Strokes"),IF(ISERROR(MATCH($B6,$B$3:$B4,0)),"Strokes",INDEX(E$3:E4,MATCH($B6,$B$3:$B4,0)+1)),""))
Now we are ready to copy the formula to the right and below as far as we need.

Drag formula down and change the reference of the column (index+match)

I need help with the following formula:
=INDEX(Sheet2!A2:A11,MATCH(Sheet1!Q5,Sheet2!C2:C11,0)+0)
(this part needs to change column references: Sheet2!C2:C11,0)+0)
I need to change the column reference whenever I'm dragging it down. I tried this:
=INDEX(Sheet2!$A$2:$A$12,MATCH(Sheet1!Q4,OFFSET(Sheet2!$A$2:$A$12,0,ROW(O$4:O4)-1),0)+0)
but it always comes up with #N/A
I tried solution from other topics but couldn't find one that uses index and match.
PS. My formula starts from cell O5
Can you advise please?
Much obliged
In general, if you want to change the column reference when dragging down, use a combination of INDEX and ROW, e.g.
= INDEX($1:$1,ROW())
This will grab values further to the right in the first row as the formula is dragged down.
You can also modify this to have INDEX return a range (instead of just a single cell) to be used as part of another formula, e.g.
= INDEX($1:$5,0,ROW())
This returns a 5x1 array which shifts over to the right as the formula is dragged down. (The 0 in the above formula indicates to select all of the rows in the $1:$5 range.)
In your formula, you can try replacing this:
Sheet2!C2:C11
With this:
INDEX(Sheet2!$2:$11,0,ROW()+<offset>)
Where <offset> is the necessary offset that you need.
If your formula starts in O5 and you want that first formula to grab the C column, I imagine that <offset> should be -2. This is because ROW() of O5 is 5, but you want that cell to grab the 3rd column (so you need to subtract 2). Then when you drag down to O6, that part of the formula would evaluate to Sheet2!D2:D11, and in cell O7, it would evaluate to Sheet2!E2:E11, etc.
So your final formula should be:
= INDEX(Sheet2!A2:A11,MATCH(Sheet1!Q3,INDEX(Sheet2!$2:$11,0,ROW()-2),0)+0)

How does the SUMPRODUCT command works in this example?

The following code allows me to determine distinct values in a pivot table in Excel:
=SUMPRODUCT(($A$A:$A2=A2)*($B$2:$B2=B2))
See also: Simple Pivot Table to Count Unique Values
The code runs perfectly fine. However, can somebody help me understand how this code actually works?
You write: the following code allows me to determine distinct values in a pivot table in Excel
No. That formula alone does not do that. Read on for the explanation of what does.
There's a typo in the formula. It should be
=SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))
See the difference?
The formula starts in row 2 and is copied down. In each row, the $A$2 reference and the $B$2 reference will stay the same. The $ signs make them absolute references. The relative references $A2 and A2 will change their row numbers when copied down, so in row 3 the A2 will change to A3 and B2 will change to B3. In the next row it will be A4 and B4, and so on.
You may want to create a sample scenario with data similar to that in the thread you link to. Then use the "Evaluate Formula" tool on the Formulas ribbon to see step by step what is calculated. The formula evaluates from the inside out. Let's assume the formula has been copied down to row 5 and we are now looking at
=SUMPRODUCT(($A$2:$A5=A5)*($B$2:$B5=B5))
($A$2:$A5=A5) this bit compares all the cells from A2 to A5 with the value in A5. The result is an array of four values, either true or false. The next bit ($B$2:$B5=B5) also returns an array of true or false values.
These two arrays are multiplied and the result is an array of 1 or 0 values. Each array has the same number of values.
The first value of the first array will be multiplied with the first value of the second array. (see the red arrows)
The second value of the first array will be multiplied with the second value of the second array. (see the blue arrows)
and so on.
True * True will return 1, everything else will return 0. The result of the multiplication is:
The nature of the SumProduct function is to sum the result of the multiplications (the product), so that is what it does.
This function alone does not do anything at all to establish distinct values in Excel. In the thread you link to, the Sumproduct is wrapped in an IF statement and THAT is where the distinct values are identified.
=IF(SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))>1,0,1)
In plain words: If the combination of the value in column A of the current row and column B of the current row has already appeared above, return a zero, otherwise, return a 1.
This marks distinct values of the combined columns A and B.
Firts, i think you made a type here, as the formula should be :
=SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))
Let's decompose it in 2 parts:
First, we check the cells between A2 and A2, so only one cell, and we check the number of cells wich are equals to A2. In this case, the output should be 1, as you're comparing A2 with A2. However, you're not limited to compare A2 with A2. If you had chosen 2 cells equals to A2, the results would have been 2.You can compare as many cells as you want with A2 (replace the characters after the $ to modulate).
We do the same for the second bracket, except the pivot value is B2.
After that, you need to understand what the function SUMPRODUCT does. It sum the value of the product for a range of array. For example, say you have the value 1 on A1, 1 on A2, 2 on B1 and 3 on B2, if you make SUMPRODUCT((A1:A2)*(B1:B2)) , you will obtain (1*2) + (1*3) = 5. So, in the example you gave us, it will give the sum of (A2=A2)*(B2=B2) = 1.
So, it will output the number of pair (Ax,Bx) which is equals to (A2,B2). With the link, you can see that, if you select the first line only, the function will output 1 (and so the IF will output 1), but if you select the first 2 lines, the function will output 2, (and so the IF will output 0).
I hope this made sense to you, as i hoped i didn't make any mistakes along the explanation.

Get non-zero value from current or previous row

I am working in Excel.
Suppose I have two columns. In first column, there are some zero and some non-zero values. The second column is blank.
If there is a non zero value in a cell of first column, the same value should be written in the next column. But if the value is a zero, then the previous non zero value in the first column should be written in the second column. So, in the end, the second column gets filled with all non zero values.
Could you help me to write the formula?
I think this is what you want. Please take a look:
https://docs.google.com/spreadsheets/d/183LFCPGfBnA0IEs4LKxk7iOgayxIFdCDaFZjFcwSU3M/edit?usp=sharing
Basically, First cell in second column, will check for zero in the first cell in first column:
=if(A1=0;"";A1)
Subsequent cells from second column will display the value of the cell above if the adjacent cell in the first column is zero:
=if(A2=0;B1;A2)
From there, is just formula extension down by the column.
Assuming you have your spreadsheet like this:
A1="Title"
A2=123
A3=0
A4=321
You could use this formula in cell B2, and copy it down:
B2=IF(A2=0, OFFSET(B2,-1,-1,1,1), A2)
Assuming your data/values start in the first column, here is an if statement that will handle it for you.
=IF(A2>0,A2,B1)

Resources