Find values that don't have a second value - excel

I am looking for a list of column A where there isn't a specific value in column B as well as any other value in Column B.
Example:
COLUMN A COLUMN B
LEFT ALPHA
LEFT BETA
LEFT CHARLIE
RIGHT BETA
RIGHT CHARLIE
UP ALPHA
UP CHARLIE
DOWN ALPHA
I want to know all Column A where there is an Alpha and at least one other value for Column A.
Left has an Alpha and another value that is not Alpha so it would be included.
Right doesn't have an Alpha so excluded.
Up has an Alpha and another value that is not Alpha so it would be included.
Down has Alpha but no other values and is excluded.
This can populate a list on another worksheet that would be best. I think I have to go the VBA route but I am unsure where to start.

This uses COUNTIFS. Adjust sheet names to your set up if necessary. I should add that you will need a header row for your data (for AF) so insert a row if necessary.
Sub x()
Dim r As Range, wf As WorksheetFunction, r1 As Range
Set wf = WorksheetFunction
Set r1 = Sheet1.Range("A1").CurrentRegion
r1.Columns(1).AdvancedFilter xlFilterCopy, , Sheet2.Range("A1"), unique:=True
With Sheet2
For Each r In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
If wf.CountIfs(r1.Columns(1), r, r1.Columns(2), "ALPHA") > 0 And _
wf.CountIfs(r1.Columns(1), r, r1.Columns(2), "<>ALPHA") > 0 Then
r.Offset(, 1) = "Yes"
Else
r.Offset(, 1) = "No"
End If
Next r
End With
End Sub

Data and result:
Formula in result A1:
=IFERROR(INDEX(Data!A$1:A$8,SMALL(IF(
MATCH(Data!A$1:A$8,Data!A$1:A$8,0)=ROW(Data!A$1:A$8),ROW(Data!A$1:A$8)),ROW())),"")
This is the same as the remove duplicated offered by excel, a formula version. Please press Ctrl + Shift + Enter to complete the formula.
Formula in result B1:
=AND(SUMPRODUCT(--(Data!A$1:A$8&Data!B$1:B$8=A1&"ALPHA"))>0,
COUNTIF(Data!A$1:A$8,A1)>1)
Just some simple criteria judgments. We build a list of combination of column A and column B. That is LEFTALPHA, LEFTBETA, LEFTCHARLIE, RIGHTBETA.... so that we can search for LEFTALPHA is exist or not. And the second criteria is find out the target is single or multiple.

Related

Dividing values in one column with another column and setting value based on another cell

I am fairly new to VBA and need some help in understanding what i am doing wrong.
I have 3 columns. If the value in the first column is zero, i want to set the third column as "N/A" . If the value in first column is not zero, then the third column should be second column / first column.
First column is c4:c6
Second column is d4:d6
Third column is e4:e6
Dim k As Range
For Each k In ws.Range("c4:c6")
If k = 0 Then
Range("e4:e6").Value = "N/A"
Else
Range("e4:e6").Formula = "=d4/c4"
End If
Next
Alternatively
Sub demo()
Range("E4:E6").FormulaR1C1 = _
"=IF(RC[-2]=0,""N/A"",RC[-1]/RC[-2])"
End Sub
Once you've defined the range you're going through, don't reference ranges again inside your loop unless you want that entire loop to be affected during each journey through the loop.
So if k=0, you don't want all of the e values to be "N/A". You only want the one related to the k cell you're looking at that has a zero.
You can use offset for this. First time through the loop, k is cell c4. So, if c4=0 then e4=N/A. e4 is the same as c4.offset(0,2). That is, it's in the same row, but two columns over.
You can also use k as a reference point to help build the formula in e, so that it's not the exact same formula each time.
With that in mind, let's rewrite your code:
Dim k As Range
Dim ws As Worksheet
Set ws = ActiveSheet
For Each k In ws.Range("C4:C6")
If k = 0 Then
k.Offset(0, 2).Value = "N/A"
Else
k.Offset(0, 2).Formula = "=" & k.Offset(0, 1).Address & "/" & k.Address
End If
Next
If you set everything in terms of k, you make sure it's all relevant, line after line.

Highlight cells with different values than present in the column

I need to go through a table (X by Y) where every column (X) should have the same value (string) and would like the cell to get highlighted if is not the same value. I'm stuck in building the comparison method, because I would like it to be dynamic. I would like to first determine what is the value that is present the most in the column and determine that is what everything else needs to be compared against and highlight the cell that is not equal to this value.
Example (6x5 table) -
A 3 4 C M R
A 3 4 O M R
8 3 T O M F
8 3 4 O M G
A 3 T O Y K
In the first column, A is the most prevalent value therefore the (8s) are highlighted, second column nothing is highlighted, third both (T) are highlighted, fourth column (C), fifth column (Y) and sixth column (F),(G) and (K) are highlighted.
Thank you in advance.
I can suggest a workaround. Not sure if this is the best way though.
Step 1. Create a count if table which basically gives you the count of a value in the column. Ex: for cell F4 use formula =COUNTIF($B$4:$B$8,B4)
Step 2. Create a row with max values of each column. Ex: Cell F11 =MAX(F4:F8)
Step 3. For each cell, give a conditional formatting condition that if the value in the count table does not match the max value, color it. Ex: for cell B4, go to conditional formatting -> new rule -> use a formula and write this formula =F4<>F$11. Then copy paste the format to all other cells.
Note: this will not work when multiple values in a column have the same max count.
Here is a VBA solution, I've commented the code so you can understand
Sub HighlightNonFrequentInColumn()
Dim rng As Range
Dim col As Range
Dim cell As Range
Dim myVal As String
Dim colRng As String
Set rng = Selection '<<change range as required
For Each col In rng.Columns
'determine the most frequent value
colRng = col.Address
On Error Resume Next
myVal = Application.Evaluate("INDEX(" & colRng & ",MODE(IF(" & colRng & "<>"""",MATCH(" & colRng & "," & colRng & ",0))))")
If Application.countblanks(col) > Application.CountIf(col, myVal) Then myVal = "" '<<if blanks are most frequent
'highlight all cells not equals to most frequent value
For Each cell In col.Cells
If Not cell = myVal Then cell.Interior.Color = vbYellow '<<change colour as desired
Next
Next
End Sub

Excel - Need to find if anything from column A is found within column B [duplicate]

This question already has answers here:
Excel Two Columns With Duplicates
(2 answers)
Closed 9 years ago.
Basicaly, I have 2 lists of email addresses in Excell...
Column A = Holds over 2051 rows of email addresses
Column B = Holds about 1994 rows of email addresses
I need a way to find out which email addresses in Column A aren't found in Column B, and preferably output the results in, either a new sheet, or in Column C.
Than after that I need to be able to find which email addresses in Column B aren't found in Column A (if any) and output that list into, either a new sheet or Column D.
How can I do this?
In either a new sheet or column C use a combination of VLOOKUP() and IFERROR() and drag that formula for every line of A.
=IF(ISERROR(VLOOKUP(A1, $B$1:$B$1995, 1, 0)), A1 & " NOT FOUND IN COLUMN B", "FOUND IN B")
This will return two different messages depending on if the e-mail was found or not in B.
Why not copy paste the data from column B onto the end of column A? Then set the conditional formatting for the column to highlight all items whose count exceeds one. Use this formula, "=countif($A$1:A1,A1)>1", without the quotes. Make sure the whole column is selected when doing this.
Another method for maintaining the separation of data. In column C use a formula like this =IF(ISERROR(VLOOKUP(A1,$B$1:$B$100,1,0)),A1,""); change the ranges to match your data ranges. Then fill down the formula until the end of data in column A. To fill down, select the desired range and press 'Cntl+D'. Repeat this for column D but swap the A and B references in the formula and fill down until the bottom of the column B data. This will result in data in columns C & D that list the unique values. Copy and paste these values, be sure to paste as values if the default paste is used Excel will paste the formulas and not the data, into another set of columns (E & F) or the same columns, then sort each column to eliminate the spaces.
You can use this Sub ..
Sub CrossCheck()
Dim LastA, LastB, r As Range
Dim x, Cn, Dn As Long
Set LastA = Range("A65536").End(xlUp)
Set LastB = Range("B65536").End(xlUp)
Cn = 1
Dn = 1
For x = 1 To LastB.Row
Set r = Columns("B").Find(Cells(x, 1), , xlValues, xlWhole)
If r Is Nothing Then
Cells(Cn, 3) = Cells(x, 1)
Cn = Cn + 1
End If
Next
For x = 1 To LastA.Row
Set r = Columns("A").Find(Cells(x, 2), , xlValues, xlWhole)
If r Is Nothing Then
Cells(Dn, 4) = Cells(x, 2)
Dn = Dn + 1
End If
Next
End Sub
Hope this help !!

Excel compare two columns and highlight duplicates

I have an excel file with 10,000 rows in column A some values are the same.
Example:
A1 - P7767
A2 - P3443
A3 - P7767
A4 - P8746
A5 - P9435
etc...
I then have another column with 100 rows which have some of the values found in column A,
B1 - P7767
B2 - P8746
etc...
I need to highlight all cells in column A where the value is found in any of the values in column B
So basically column B checks to see if it can find the same value anywhere in column A, if true highlight the cell leaving any cells white when the value is not found in column B
I hope I have explained this well, I have done some research and I believe I need to use conditional formatting to get this result but I am really stuck on the formula to use and cannot seem to find an example online (Maybe I am not searching the correct term as I'm not sure on what this is exactly called)
There may be a simpler option, but you can use VLOOKUP to check if a value appears in a list (and VLOOKUP is a powerful formula to get to grips with anyway).
So for A1, you can set a conditional format using the following formula:
=NOT(ISNA(VLOOKUP(A1,$B:$B,1,FALSE)))
Copy and Paste Special > Formats to copy that conditional format to the other cells in column A.
What the above formula is doing:
VLOOKUP is looking up the value of Cell A1 (first parameter) against the whole of column B ($B:$B), in the first column (that's the 3rd parameter, redundant here, but typically VLOOKUP looks up a table rather than a column). The last parameter, FALSE, specifies that the match must be exact rather than just the closest match.
VLOOKUP will return #ISNA if no match is found, so the NOT(ISNA(...)) returns true for all cells which have a match in column B.
A simple formula to use is
=COUNTIF($B:$B,A1)
Formula specified is for cell A1. Simply copy and paste special - format to the whole of column A
NOTE: You may want to remove duplicate items (eg duplicate entries in the same column) before doing these steps to prevent false positives.
Select both columns
click Conditional Formatting
click Highlight Cells Rules
click Duplicate Values (the defaults should be OK)
Duplicates are now highlighted in red:
The easiest way to do it, at least for me, is:
Conditional format-> Add new rule->Set your own formula:
=ISNA(MATCH(A2;$B:$B;0))
Where A2 is the first element in column A to be compared and B is the column where A's element will be searched.
Once you have set the formula and picked the format, apply this rule to all elements in the column.
Hope this helps
A1 --> conditional formatting --> cell value is B1 --> format: whatever you want
hope that helps
Suppose you want to compare a column A and column H in a same spreadsheet .
You need to go another column next to these 2 columns and paste this formula :
=(Sheet1!A:A=Sheet1!H:H)
this will display FALSE or TRUE in the column . So you can use this new column to color the non matching values using conditional color formatting feature .
I was trying to compare A-B columns and highlight equal text, but usinng the obove fomrulas some text did not match at all. So I used form (VBA macro to compare two columns and color highlight cell differences) codes and I modified few things to adapt it to my application and find any desired column (just by clicking it). In my case, I use large and different numbers of rows on each column. Hope this helps:
Sub ABTextCompare()
Dim Report As Worksheet
Dim i, j, colNum, vMatch As Integer
Dim lastRowA, lastRowB, lastRow, lastColumn As Integer
Dim ColumnUsage As String
Dim colA, colB, colC As String
Dim A, B, C As Variant
Set Report = Excel.ActiveSheet
vMatch = 1
'Select A and B Columns to compare
On Error Resume Next
Set A = Application.InputBox(Prompt:="Select column to compare", Title:="Column A", Type:=8)
If A Is Nothing Then Exit Sub
colA = Split(A(1).Address(1, 0), "$")(0)
Set B = Application.InputBox(Prompt:="Select column being searched", Title:="Column B", Type:=8)
If A Is Nothing Then Exit Sub
colB = Split(B(1).Address(1, 0), "$")(0)
'Select Column to show results
Set C = Application.InputBox("Select column to show results", "Results", Type:=8)
If C Is Nothing Then Exit Sub
colC = Split(C(1).Address(1, 0), "$")(0)
'Get Last Row
lastRowA = Report.Cells.Find("", Range(colA & 1), xlFormulas, xlByRows, xlPrevious).Row - 1 ' Last row in column A
lastRowB = Report.Cells.Find("", Range(colB & 1), xlFormulas, xlByRows, xlPrevious).Row - 1 ' Last row in column B
Application.ScreenUpdating = False
'***************************************************
For i = 2 To lastRowA
For j = 2 To lastRowB
If Report.Cells(i, A.Column).Value <> "" Then
If InStr(1, Report.Cells(j, B.Column).Value, Report.Cells(i, A.Column).Value, vbTextCompare) > 0 Then
vMatch = vMatch + 1
Report.Cells(i, A.Column).Interior.ColorIndex = 35 'Light green background
Range(colC & 1).Value = "Items Found"
Report.Cells(i, A.Column).Copy Destination:=Range(colC & vMatch)
Exit For
Else
'Do Nothing
End If
End If
Next j
Next i
If vMatch = 1 Then
MsgBox Prompt:="No Itmes Found", Buttons:=vbInformation
End If
'***************************************************
Application.ScreenUpdating = True
End Sub
Don't wana do soo much work guyss..
Just Press Ctr and select Colum one and Press Ctr and select colum two.
Then click conditional formatting -> Highlight Cell Rules -> Equel To.
and thats it. your done. :)

Excel formula to find the rightmost column containing a value in a table

I have some data structured like this in an Excel spreadsheet:
A B C D E F
1 1 1 2 x 2 3
2 1 1 1 2 2 3
3 3 3 3 3 4 4
I am trying to formulate an Excel formula which will give me the index of the rightmost column in this table which has a cell matching a specific value.
In this example, the rightmost column containing the value '1' is in column C. For '2' it would be E. That column index is what I'm after.
I use column letters to be Excel-consistent, but a numeric column index is preferable.
I have tried some other solutions for similar Excel problems found online, but they don't quite have the right behavior.
If you want to use helper columns, you can put this formula in G1
{=MAX((COLUMN(A1:F1)*(A1:F1=2)))}
That's array entered. Fill down to G3. In G4, put
=MAX(G1:G3)
Then repeat for each number. If you don't want helper columns, you can write a UDF like this
Public Function MaxColumn(rInput As Range, vValue As Variant) As Long
Dim rFound As Range
Set rFound = rInput.Find(vValue, rInput.Cells(1), xlValues, xlWhole, xlByColumns, xlPrevious)
If Not rFound Is Nothing Then MaxColumn = rFound.Column
End Function
Which you call like
=maxcolumn(A1:F3,2)
Here is a way to do it with formulas. I'll show how to do it with a few different formulas to show the steps of the logic, and then put them together into one big formula.
First, use one formula per column to see if the target value is in the column. For example in column A:
=COUNTIF(A1:A100,Goal)
=COUNTIF(B1:B100,Goal)
...
(where Goal can be a hardcoded search value,
or a named range where you type your query)
Then, add IF statements to these formulas to translate this into column numbers. If the query is present in the column, show the column number, else show zero.
=IF(COUNTIF(A1:A100,Goal)>0, 1, 0)
=IF(COUNTIF(B1:B100,Goal)>0, 2, 0)
...
Finally, add a formula to grab the maximum column number from the prior formulas. This will equal the rightmost column with your query value in it.
=MAX( IF(COUNTIF(A1:A100,Goal)>0, 1, 0), IF(COUNTIF(B1:B100,Goal)>0, 2, 0), ...)
How about:
Function FindCol(ToFind)
Dim r As Range
Dim rfind As Range
Dim rfound As Range
Set r = ActiveSheet.UsedRange
For i = r.Columns.Count To 1 Step -1
Set rfind = r.Columns(i)
Set rfound = rfind.Find(ToFind)
If Not rfound Is Nothing Then
Result = rfound.Column
Exit For
End If
Next
FindCol = Result
End Function
Here's a solution using the new LAMBDA and BYROW functions.
The BYROW looks at each line in a set range and returns the right-most column index number where the goal number is present. The IFERROR is used to catch any errors where the goal number isn't present, and the MAX is used to return the highest number/rightmost indexed column.
=LET(
goal, $H$1,
range, A1:F3,
MAX(IFERROR(BYROW(range,LAMBDA(x,XMATCH(TRUE,INDEX(x=goal,),0,-1))),0)))
Full Tutorial Here

Resources