VBA - Remove duplicate values in lookup

slyhook

New Member
Joined
Mar 21, 2011
Messages
1
I am using a VBA function to take a cell, look it up on another sheet and return (and combine) the data from several columns on that other sheet. Works, except that it returns duplicate values. Any ideas on fixing that much appreciated.

Example of what I currently get. Used "•" to denote TAB -- Sorry, wasn't sure how else to show it

Key • Result1 • Result2 • Result3 • Etc…
A1 • ABC; ABC; AB3 • RF45; LF67; LF67 • opn; opn; opn
A1 • ABC; ABC; AB3 • RF45; LF67; LF67 • opn; opn; opn
A1 • ABC; ABC; AB3 • RF45; LF67; LF67 • opn; opn; opn
BB2 • BCD; BCD • GP909; N54 • wet2; cls
CCC3 • CDE; CDE; CDE • PR1; PR1; PR2 • ptl; ptl; ptl
CCC3 • CDE; CDE; CDE • PR1; PR1; PR2 • ptl; ptl; ptl
CCC3 • CDE; CDE; CDE • PR1; PR1; PR2 • ptl; ptl; ptl
A1 • ABC; ABC; AB3 • RF45; LF67; LF67 • opn; opn; opn
CCC3 • CDE; CDE; CDE • PR1; PR1; PR2 • ptl; ptl; ptl
Etc…

What I want-- no duplicated data

A1 • ABC; AB3 • RF45; LF67 • opn
A1 • ABC; AB3 • RF45; LF67 • opn
A1 • ABC; AB3 • RF45; LF67 • opn
BB2 • BCD • GP909; N54 • wet2; cls
CCC3 • CDE • PR1; PR2 • ptl
CCC3 • CDE • PR1; PR2 • ptl
CCC3 • CDE • PR1; PR2 • ptl
A1 • ABC; AB3 • RF45; LF67 • opn
CCC3 • CDE • PR1; PR2 • ptl

Here is the code I am using -- I have tried a number of things, but none working for me.

Public Function MPartsLOOKUP(lookup_value As Variant, table_array As Range, _
col_index_num As Long) As Variant

Application.Volatile (False)
Dim Cell As Range
Dim A, B
A = ""
MPartsLOOKUP = CVErr(xlErrNA)

Set table_array = Intersect(table_array, table_array.Parent.UsedRange)
If table_array Is Nothing Then Exit Function

For Each Cell In Union(table_array.Columns(1), table_array.Cells(1))
If Cell = lookup_value Then
If A <> "" Then
A = A & "; " & Cell.Offset(0, col_index_num - 1)
GoTo 10
End If


A = Cell.Offset(0, col_index_num - 1)
End If

10 Next Cell
If A = "" Then MPartsLOOKUP = ""
MPartsLOOKUP = A

End Function

Starting in second column, I am entering =MPartsLOOKUP($A2,TestRng,2)

Sheet where data is looked up looks like this:

Key • Data1 • Data 2 • Data3
A1 • ABC • RF45 • opn
A1 • ABC • LF67 • opn
A1 • AB3 • LF67 • opn
BB2 • BCD • GP909 • wet2
BB2 • BCD • N54 • cls
CCC3 • CDE • PR1 • ptl
CCC3 • CDE • PR1 • ptl
CCC3 • CDE • PR2 • ptl

Any help very much appreciated. Please let me know if I need to clarify anything!

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top