Excel formatting/Highlight row based on the last date and equal cell value - excel

My question relates with the following.
I have a matrix in Excel of values, I want to highlight the entire row if two conditions are met. Those conditions are:
a. For equal values on column A compare its date;
b. If the value is equal, highlight the row with the most recent date.
Graphically its the following:
A B
1 Y 04-08-2006
2 X 02-07-2008
3 X 12-05-2014
4 Z 09-08-2014
5 X 25-06-2016
6 Z 01-04-2018
7 Y 24-07-2018
8 X 25-09-2018
I want to highlight the 6, 7 and 8 row with different colors.
Can I do that with conditional formatting or its not possible and the only way is to write code in VBA?
Thank you.

You don't need VBA for this. Make a small Pivot table where your column A is the rows and columns B are the values set to max. Then add a columns that is a vlookup to add the max date for each group (formula show in image) and finally just add conditional formatting, one rule per group again as shown in the image (note in this case the formatting is applied to the range A1:C9
You can always hide column D and the pivot table or put them on another sheet if need be.

Related

Excel: Conditional formatting of whole row based on column value does not work

Suppose I have a simple dataset in Excel:
Column 1 Column 2
A 1
B 1
C 2
D 4
E 5
F 9
Now I want to mark the whole row with green color, if the value in column 2 is larger than 3. I apply a conditional format with the formula =$B2>3 applied on range =$A$1:$B$7 and it does not work:
One line where the value is 2 is marked green and one where it is 9 is not marked.
Now I want to mark the row, however only, if the value in column 2 is between 3 and 6. I apply the formula =AND(3<$B2;$B2<6) to the same range and it does not work:
Nothing is marked green.
Where is my mistake?
Update:
I now also tried =AND(3<$B1;$B1<6), but still nothing is marked green?
Change =$B2>3 to =$B1>3 which should work for you.
Your formula start range and apply start range must be same. Otherwise CF will highlight different cells.

Number occurrences in another cell (Excell) [duplicate]

I have simple problem, but I've not be able to get an answer from searching. I require a column to calculate the number of the nth occurrence of a value. It's best explained in this picture
I require a method to calculate column B.
I'd be very grateful for any help.
Are you looking to merely provide a count of the distinct entries of column A in column B? Or merely add a formula to come up with the table in your link?
If the latter, then the formula to write in cell B2 is:
=COUNTIF(A$2:A2,A2)
then copy/paste it down column B. Note - if your data is both a Date and Time, but the cell is formatted to only display a date, you may not get the results you want. You'd need to interject a new column with a "floor" calculation to round the date/time value to a date (Excel date times are decimal, with integer part dictating the date, and remaining 0.0 -> 1.0 dictating the time of day)
If you just want to derive a table of the counts of distinct entries in column A, then a pivot table will do this for you - simple add a pivot table to cover the data in column A, then select column A into the rows category, and then also drag it into the values category, ensuring the field is set to "Count of". You should then have a table with the distinct entries in your data set in one column, and the count of their occurrences in the other column.
You can use the COUNTIF worksheet function, with a relative address.
Eg. In cell B2, enter this formula:
=COUNTIF(A$2:A2,A2)
And then fill-down.
Use the following formula to generate the required series:
=COUNTIF($A$1:A1,A1) and strech(copy) it in all the cells
This will generate result like this:
A 1 COUNTIF($A$1:A1,A1)
A 2 COUNTIF($A$1:A2,A2)
C 1 COUNTIF($A$1:A3,A3)
C 2 COUNTIF($A$1:A4,A4)
B 1 COUNTIF($A$1:A5,A5)
B 2 COUNTIF($A$1:A6,A6)
A 3 COUNTIF($A$1:A7,A7)
C 3 COUNTIF($A$1:A8,A8)
D 1 COUNTIF($A$1:A9,A9)
D 2 COUNTIF($A$1:A10,A10)
D 3 COUNTIF($A$1:A11,A11)
D 4 COUNTIF($A$1:A12,A12)

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

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))),";"))

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.

Compare in two columns and add value

I have 2 columns looking like this:
Column 1 Column 2
1 x
x 2
2 2
x x
1 2
I want to do two things;
For each row match (row n column 1 = row n column 2) it should mark cell n in column 1 green if there is a match and red if it is not.
It should create a sum cell where each match is worth 1 point, in this case column 1 should result in 2 points.
Is this even possible with excel and if so, how is it done?
For the first part of your question:
It should mark cell n in column 1 green if there is a match and red if it is not
You can do this using Conditional Formatting.
Ex:
Assume column A and column B, with the values starting in row 2.
The following conditional formatting would highlight column A values in green if they match the corresponding value in column B in the same row, otherwise red.
Highlight the values in column A, then apply this conditional formatting.
For the second part of your question
It should create a sum cell where each match is worth 1 point, in this case column 1 should result in 2 points
The following array formula will tally all the matches and show you how many there are:
=SUM(IF(A2:A6=B2:B6,1,0))
Assuming again that we are in columns A & B with your sample data.
Remember to commit this formula using Ctrl+Shift+Enter.
Per comment from andy holaday, here is another formula that will work:
=SUMPRODUCT(N(A2:A6=B2:B6))
or
=SUMPRODUCT(--(A2:A6=B2:B6))
These are not CSE formulas so you would not need Ctrl+Shift+Enter to commit them.

Resources