There are a few questions that ask something similar but not the exact thing.
I have two columns X and Y. Y contains only values that exist in X. I want to create a column Z that has all the values that exist only in X.
XandYcan contain duplicate data as shown in the exampleXexists insheet1whilstY and Zexist insheet2
| X | Y | Z |
|---|---|---|
| a | c | a |
| b | e | b |
| b | d | |
| c | e | |
| d | ||
| e |
So far, I recorded a macro so naturally the code is super slow, despite my best efforts to clean it up. I won't post the whole code because it's quite messy but essentially I've
Used the
unique()function to create two columns that contain the unique values ofXandYrespectively.Used
vlookup()to create an adjacent column to the two I just created that returns an empty string if the adjacent uniqueXvalue exists in the uniqueYcolumn else returning theXvalue. This part is horribly slow. I created the formula in one cell then pasted it down.
Range("U2").Formula2R1C1 = "=UNIQUE('1.HoldingCart'!C[-18])"
Range("V2").Formula2R1C1 = "=UNIQUE(C[-19])"
Range("W3").FormulaR1C1 = "=IF(ISNA(VLOOKUP(RC[-2], C[-1], 1, FALSE)), RC[-2], """")"
Range("W3").Copy
Range("W3:W" & Cells(Rows.Count, "U").End(xlUp).Row).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
- Filtered out all the empty strings on the
vlookup()column. Copied the actual values. Got rid of the filter. Deleted everything and then pasted the copied data thus creating columnZ.
' Get the discrepancies
ActiveSheet.Range("$W:$W").AutoFilter Field:=1, Criteria1:="<>"
Range("W2:W" & Cells(Rows.Count, "W").End(xlUp).Row).Copy
Range("X2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=False
' Clean the sheet
ActiveSheet.ShowAllData
Selection.AutoFilter
Range("U2:W" & Cells(Rows.Count, "W").End(xlUp).Row).ClearContents
' Paste the discrepancies
Range("X2:X" & Cells(Rows.Count, "X").End(xlUp).Row).Cut
Range("U2").Select
ActiveSheet.Paste
Sorry you just had to read that horrible code. I'm happy to throw all that away. Any help would be appreciated.



