How to highlight nonmatching values based on certain column values in VBA - excel

How would I go about implementing a macro or conditional formatting rule that would compare two separate sheets based on their values in a column?
Basically, what I need to do is search each sheet for the matching values (e.g. cell A10 would be "project 10" and on the other sheet it might be cell A6 as "project 10").
Then it would look at a separate column, and match the dates there (i.e, cell A10 would have date "11/12/15" and cell A6 on the other sheet would have a date of "11/11/15") highlighting the cell if it is different than the date on the first sheet.
The tricky thing here is that the projects are in a different order on each sheet (as explained above). I have attempted several code suggestions but have not gotten anywhere yet.

Let's solve using conditional formatting.
As you may be aware, conditional formatting works by setting up a rule which governs the cell you apply it to. If that rule produces TRUE, then the rule is applied (diff colour, etc). So the goal here is to make a formula which results in TRUE when all of your conditions are met. There are two conditions we care about here. First, does the item in column A on sheet1 exist in column A on sheet2?
=not(iserror(match($A1,sheet2!$A:$A,0)))
First, it uses the MATCH function to check if A1 (with an absolute reference on the column, so that as you apply the rule to columns A & B it will not look at column B at all) exists on sheet2 columnA. If it does exist, it will spit out the row number that it appears at. If it does not exist, it will create an error. So that is wrapped in ISERROR which says "if this argument returns an error, spit out TRUE". But, we want it to return TRUE when there is no error. So, we wrap that in NOT, which reverses TRUE to FALSE, and vice versa.
Now what you actually care about is, if A1 exists in sheet2, then does that row in column B on sheet2 match the value in B1 on sheet1?
So we find the value in column B, where there is a match in column A, by using the index function, like so:
=Index(sheet2!$B:$B,match($A1,sheet2!$A:$A,0))
This gives that value from column B in sheet 2. So, check: does that value equal the value in B1, sheet1? [We actually check to see if that value is DIFFERENT than B1, so we use "<>" instead of "=".
=Index(sheet2!$B:$B,match($A1,sheet2!$A:$A,0))<>$B1
Now we just need to replace an error (if Match returns no match) with "".
=Iferror(Index(sheet2!$B:$B,match($A1,sheet2!$A:$A,0)),"")<>$B1
And that's it. If there's a match in sheet2 columnA [using the MATCH function] it gives you the date from column B [using the INDEX] function. If there's no match, it returns "" [using the IFERROR function]. And then if that value does NOT equal B1, then it results in TRUE, which triggers your conditional formatting.

Related

Using nested IF function in excel: is there a way to check next row for same IF function?

Let's say I have a simplified table of only 2 columns. Column one is Group number, with groups of different sizes. Column 2 is also numbered but has a lot of blanks.
I basically want to fill these blanks with the next NOT blank, if it is still in the same first-column-group.
I don't know if it works to show a table here, but this is what it is now and what I want to achieve with a simplified dataset:
What I tried is to use an If function. First, it checks if B1 is blank. If it is blank, I want it to check if A1 is the same "group" value as A2. If that is also true, then I want the formula to return me the value of B2. However, if B2 is also blank I want it to loop: Check if A1/A2 are the same as A3, and then give the value of B3. And continue. Plus! If B1 is not blank to begin with it just needs to return the value B1. And if B1 is blank but A1 is not the same group as A2, I want it to remain blank (or 0 is also fine).
The only way I did this was making an extremely nested if function that kept repeating for the [if true] part, which looks like this:
=IF(B2="";(IF(A2=A3;
(IF(B3="";(IF(A3=A4;
(IF(B4="";(IF(A4=A5;
(IF(B5="";(IF(A5=A6;
(IF(B6="";(IF(A6=A7;
(IF(B7="";(IF(A7=A8;
(IF(B8="";(IF(A8=A9;"xx";""))
;B8));""));B7));""));B6));""));B5));""));B4));""));B3));""));B2)
However, sometimes there are 100+ blanks within one group, and I'm trying to avoid looping 100+ repetitions of that formula.Right now, when there are not enough loops for within a group, it shows the xx. Is there no way to just replace the B1 by a continuous string of cells of column B? I looked at the Row function but it only works when you pull it down if I understand it correctly.
Would really appreciate the help!
If you did want to try a formula, something like this will work:
=IF(B2<>"",B2,IFERROR(INDEX(B2:B$19,MATCH(A2,IF(B2:B$19<>"",A2:A$19),0)),""))
entered as an array formula using CtrlShiftEnter in my version of Excel (2019).
The inner IF statement produces an array where only the rows that have a number in column B will have a value equal to the group value in column A, otherwise it will default to FALSE, so you get
False
False
1
False
1
False
False
2
False
...
then you do a Match on the value of A which is in the current row in the array to find the next row where there is a non-blank value in column B (3 in the first case). Then Index produces the corresponding value from column B in the row found by the Match (1 in the first case).
If the match fails because there are no further entries in the current group (like in rows 15 and 16 of the screenshot) then it will call in the "" from the IFERROR statement, so you get a blank in that row.
You could change the IFERROR to zero if you preferred to have zeroes in those cells rather than blanks:
=IF(B2<>"",B2,IFERROR(INDEX(B2:B$19,MATCH(A2,IF(B2:B$19<>"",A2:A$19),0)),0))

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.

Excel If cell is empty want to move data from another cell

I wish to check if a cell in say column A is empty and if it is then move the data from the next column B into it. Else nothing done.
I wish it to check 1500 rows. I tried
If(A2="",A2=B2,"") I thought I could just copy it down the column. I get a circular error reference and also then cellA2 changes to 0.
Thank you for your help.
Is this a one-time task, merging two columns of data, or something that needs to be run repeatedly?
Your formula won't work (partly) because you can't have a cell that contains both a value and a formula (to check that value), at the same time.
You could, however have a 3rd column that equals Column A, unless Column A is blank, in which case it looks at Column B.
With your example, in cell C2 you could put:
=If(A2="",B2,A2)
With your formula above, you're trying assign a value to A2 with A2=B2. Excel formulas in cells assign values to "themselves" and cannot directly affect other cells.
A formula in A2 can only affect the A2. (However, the value of a cell can indirectly affect other cells with the help of VBA or features like Conditional Formatting.)
The reason your If statement isn't giving you an error with A2=B2 is because that statement A2=B2 is comparing the value of cell A2 to B2 and returning TRUE if the cells match, and FALSE if they do not (so will always return FALSE in this case).

Excel: Search A Cell To See If A Cell From An Entire Column Is Present

Having trouble with a formula to solve this problem. I can do it when comparing one cell but can't figure out how to do it to check the entire column.
What I have two columns. Column A is a list of keywords. Column B is a list of Cities.
I want to compare all of Column B to see if any of those cities are contained on a cell by cell basis in column a
If I just use =ISNUMBER(SEARCH($B$2,A2)) it will compare all the cells in column A to B2 and the formula works. But I have a few hundred cells in Column B that I want to compare A2 too. I want to know if any of the words in Column B show up in the cell A2
Instead of just $B$1 I'd like to compare the entire B column (Like B:B but that doesn't work) and see if any of the words in Column B are in Cell A2.
Hope that makes sense.
Using the whole column B:B is not possible because of performance issues. But you could using a part of the column $B$2:$B$100 within an array formula.
Example:
Formula in English notation:
{=SUM(--ISNUMBER(SEARCH($B$2:$B$67,A2)))}
This is an array formula. Input it into the cell without the curly brackets and then press Ctrl+Shift+Enter to confirm. The curly brackets will then appear automatically.
How it works:
In array context ISNUMBER(SEARCH($B$2:$B$67,A2)) takes all values of $B$2:$B$67 into the formula and results in an array of {TRUE, FALSE, FALSE, TRUE, ...} dependent of whether the value is found in A2 or not. The -- gets the boolean values as numeric 0 or 1. The SUM then sums the array. So it counts how often 1 will be present.
Edit:
This needs the $B$2:$B$67 filled with values because otherwise the "" from empty cells will always be found. So to be more flexible we should check whether cells in $B$2:$B$1000 are empty and excluding those then.
{=SUM(($B$2:$B$1000<>"")*ISNUMBER(SEARCH($B$2:$B$1000,A2)))}

How to look up value in a column, and assign specific entries

I have a sheet in Excel where the columns contain different names of people that are in different teams in my department (i.e. Names in Column 1 are in Team 1).
For another sheet, I need to have a formula that achieves the following:If I write a name in Cell B2 that can be found in the first column of that other sheet (Team 1), Excel should populate cell B6 with "Team 1".
If instead, the name I wrote is found in the second column, then the text should read "Team 2".
I've tried a couple of different formulas w/o success, and stringing a lot of IF and OR functions together is way too cumbersome. Anybody has a better solution?
If you need this formula for more then only two column then id use the formula
=INDEX(Sheet1!C1:G1,SUMPRODUCT((Sheet1!C2:G6=B1)*COLUMN(Sheet1!C2:G6)))
Say you have a set-up like:
And in B1 of Sheet2 you enter Name3, you want it to return TEAM1 as shown below:
The only catch to this formula is that you will need to tell it how many columns are before your data so if you have a table more like:
Then you need to tell the formula that there are 2 rows before your data starts, your new formula will be
=INDEX(Sheet1!C1:G1,SUMPRODUCT((Sheet1!C2:G6=B1)*COLUMN(Sheet1!C2:G6))-2)
Notice the -2 before the last parenthesis this is to indicate that there are 2 columns BEFORE you data.
and the new result for looking up say NAME20 would become as follows:
*EXPLANATION: *
The formula works as follows , I will do it all on one page for easier viewing and instead of Sheet2!B2 and B6 I will use G2, and G6 on the same sheet as the data.
First we look at all values in the Range A1:E6 and find the one with the matching name so we use
=A1:E6=G2
Now when you enter this into the cell you will recieve a #VALUE,but if you goto the Formula Bar and press F9 it will shwo you the full value of the formula
{FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,TRUE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE;FALSE,FALSE,FALSE,FALSE,FALSE}
In this we know that one cell contains the Name we searched for, this is represented by the TRUE
Now when using a formula like SUMPRODUCT, FALSE = 0 and TRUE = 1
So to get the column we mutiply every result by the column they are in.
=SUMPRODUCT((A1:E6=G2)*COLUMN(A1:E6))
So any cells that don't =G2 are FAlSE or 0, and when you multiply the column of that cell by 0 (because false = 0) it will simply result in 0. But for the one cell that IS TRUE or 1 When you multiply the column of that cell by 1 (because TRUE = 1) The product will be the cells column number
So as shown in the picture when looking for NAME13 the result of the sumproduct is 3 (its column)
Then to return the TEAM for that name, we use the INDEX function will return the value in a cell at the given cordinates.
In this case we want the value from A1:E1 that is in the same column as the Name we matched. When looked at as an Array A1:E1 looks like
{"TEAM1","TEAM2","TEAM3","TEAM4","TEAM5"}
And we want to return the value in that array at the same position as the column number of our cell, Minus any nonincluded column before the data, thus the reason for the -2 in the secon example I gave you.
So:
=INDEX(A1:E1,SUMPRODUCT((A1:E6=G2)*COLUMN(A1:E6)))
In this example excel interprets this formula as
Return the 3rd value (the 3 comes from the sumproduct as explained earlier) in the list of value from A1:E1. And that value would be TEAM3.
use a MATCH() or VLOOKUP() function inside an IF(). SOmthing like:
IF(NOT(ISERROR(MATCH(...)));"Team1";"")
Did you know you can browse functions by catergoty (such as Search), just clikc the fx left of your formula bar and use the drop down box. Each formula comes with a description with usually rather clear information...

Resources