Sub HandlingDuplicates()
Dim List1 As Range, List2 As Range
Dim LoopList As Range, SearchList As Range
Dim DuplicateList As Range, UniqueList As Range
Dim StartRow As Long
StartRow = 1 '<--Change this to beginning of your lists
Set List1 = Range(Cells(StartRow, "C"), Cells(Rows.Count, "C").End(xlUp))
Set List2 = Range(Cells(StartRow, "D"), Cells(Rows.Count, "D").End(xlUp))
Set DuplicateList = Range("F1")
Set UniqueList = Range("G1")
DuplicateList.Value = "DUPLICATES"
UniqueList.Value = "UNIQUE"
If List1.Rows.Count > List2.Rows.Count Then
Set LoopList = List2
Set SearchList = List1
Else
Set LoopList = List1
Set SearchList = List2
End If
Application.ScreenUpdating = False
For Each c In LoopList.Cells
If Application.WorksheetFunction.CountIf(SearchList, c.Value) >= 1 Then
Set DuplicateList = DuplicateList.Offset(1, 0).Resize(1, 1)
DuplicateList = c.Value
'Set DuplicateList = DuplicateList.Resize(DuplicateList.Rows.Count + 1, 1)
Else
Set UniqueList = UniqueList.Offset(1, 0).Resize(1, 1)
UniqueList = c.Value
End If
Next c
Application.ScreenUpdating = True
End Sub
jsiason said:hi ben, can this be done using function (and not using macros)?
easiest steps that will produce this output...
Also, in column G, i meant list unique values including those that appear in C and D, just that they will appear only once as one item in column G.
book1 | |||||||
---|---|---|---|---|---|---|---|
C | D | E | F | G | |||
1 | LIST1 | LIST2 | COMBINED | UNIQUE | |||
2 | a | a | a | a | |||
3 | b | b | a | b | |||
4 | c | c | b | c | |||
5 | d | x | b | d | |||
6 | e | y | c | e | |||
7 | f | z | c | f | |||
8 | g | z | d | g | |||
9 | e | x | |||||
10 | f | y | |||||
11 | g | z | |||||
12 | x | ||||||
13 | y | ||||||
14 | z | ||||||
15 | z | ||||||
Sheet1 |
Book7 | ||||||||
---|---|---|---|---|---|---|---|---|
C | D | E | F | G | H | |||
1 | 0 | 3 | 9 | |||||
2 | List-1 | List-2 | Idx | CommonList | Merged Distinct List | |||
3 | a | b | 1 | a | a | |||
4 | c | d | e | b | ||||
5 | e | e | 2 | b | c | |||
6 | f | g | d | |||||
7 | h | a | e | |||||
8 | b | i | 3 | f | ||||
9 | g | |||||||
10 | h | |||||||
11 | i | |||||||
12 | ||||||||
Sheet1 |