PDA

View Full Version : List Unique/Common Values From Two Ranges



Admin
09-16-2011, 08:34 AM
Hi All,

Here is a UDF, which gives you either UNIQUE values or COMMON values from TWO ranges based on the 'StrType' parameter.

Paste the code in a Standard module.


'// Developed by Kris @ ExcelFox.com on 15-Sep-2011

Public dicU As Object
Public dicC As Object
Function GETVALUES(ByRef V1, ByRef V2, Optional ByVal StrType As Long = 0, _
Optional ByVal Delim As String = ", ") As String

Dim r As Long
Dim c As Long
Dim d As Object

'//StrType = 0 (For Unique Values)
'//StrType = 1 or any non zero number (For Common Values)

If TypeOf V1 Is Range Then V1 = V1.Value
If TypeOf V2 Is Range Then V2 = V2.Value

If dicC Is Nothing Then
Set dicC = CreateObject("scripting.dictionary")
dicC.comparemode = 1
End If
If dicU Is Nothing Then
Set dicU = CreateObject("scripting.dictionary")
dicU.comparemode = 1
End If

If IsArray(V1) Then
For r = 1 To UBound(V1, 1)
For c = 1 To UBound(V1, 2)
If Not dicU.exists(Trim$(V1(r, c))) Then
dicU.Add Trim$(V1(r, c)), 1
End If
Next
Next
Else
dicU.Add Trim$(V1), 1
End If
If IsArray(V2) Then
For r = 1 To UBound(V2, 1)
For c = 1 To UBound(V2, 2)
If StrType = 0 Then
If Not dicU.exists(Trim$(V2(r, c))) Then
dicU.Add Trim$(V2(r, c)), 2
End If
Else
If dicU.exists(Trim$(V2(r, c))) Then
If dicU.Item(Trim$(V2(r, c))) = 1 Then
dicC.Item(Trim$(V2(r, c))) = Empty
End If
End If
End If
Next
Next
Else
If StrType = 0 Then
If Not dicU.exists(Trim$(V2)) Then
dicU.Add Trim$(V2), 2
End If
Else
If dicU.Item(Trim$(V2)) = 1 Then
dicC.Item(Trim$(V2)) = Empty
End If
End If
End If

If StrType = 0 Then
If dicU.Count Then GETVALUES = Join$(dicU.keys, Delim)
Else
If dicC.Count Then GETVALUES = Join$(dicC.keys, Delim)
End If

End Function


Here is an example..


<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:128px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >*</td><td >A</td><td >B</td><td >C</td><td >D</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="text-align:right; ">1</td><td style="text-align:right; ">1</td><td >*</td><td >1, 2, 3, 4, 5, 6, 7, 8, 9</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:right; ">2</td><td style="text-align:right; ">2</td><td >*</td><td style="text-align:right; ">1, 2, 6</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">3</td><td style="text-align:right; ">7</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">4</td><td style="text-align:right; ">8</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">5</td><td style="text-align:right; ">9</td><td >*</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:right; ">6</td><td style="text-align:right; ">6</td><td >*</td><td >*</td></tr></table><br /><table style="font-family:Arial; font-size:10pt; border-style: groove ;border-color:#00ff00;background-color:#fffcf9; color:#000000; "><tr><td ><b>Spreadsheet Formulas</b></td></tr><tr><td ><table border = "1" cellspacing="0" cellpadding="2" style="font-family:Arial; font-size:9pt;"><tr style="background-color:#cacaca; font-size:10pt;"><td >Cell</td><td >Formula</td></tr><tr><td >D1</td><td >=GETVALUES&#40;A1&#58;A6,B1&#58;B6,0&#41;</td></tr><tr><td >D2</td><td >=GETVALUES&#40;A1&#58;A6,B1&#58;B6,2&#41;</td></tr></table></td></tr></table> <br /><br /><span style="font-family:Arial; font-size:9pt; font-weight:bold;background-color:#ffffff; color:#000000; ">Excel tables to the web &#62;&#62; </span><a style ="font-family:Arial; font-size:9pt; color:#fcf507; background-color:#800040; font-weight:bold;" href="http://www.excel-jeanie-html.de/index.php?f=1" target="_blank"> Excel Jeanie HTML 4 </a>



Enjoy !!