Tr3yAnderson
New Member
- Joined
- Aug 31, 2015
- Messages
- 16
I've tried many options I found online without any luck. Basically I have written and Function for when doing a vlookup/index-match, if there are multiple instances of your lookup value it will loop through each instance and Concatenate each result into the cell. For example if I want to lookup based on the date 3/3/2020 there are two names associated with that date. The Function I've written can handle that portion and will return "Blake, Anderson", where it fails is if have an instance where I lookup 2/27/20, this has 4 instances of the same name. I don't want to return 4 instances of the same name but to delete the duplicates within my array. I've searched the web and found most use the Dictionary method as you'll see I have 3 of those options I've tried below. the problem is it doesn't get rid of all the duplicates. it'll make the array ("Johnson, Johnson, Johnson, Johnson") and reduce it to only ("Johnson, Johnson"). Any Ideas? I'll readily admit I don't understand the Dictionary method well enough to fully grasp what's going on. Additionally if there is a better way to even accomplish my initial task I'm open for that as well.
*Notes on the variables
Lookupvalue = the date you are initially looking for
LookupRange = the column range where the dates are in
TableRange = the entire table array
ColumnNum = the column # within the TableRange that has the name that you want to concatenate
Function MLOOKUPCONCATENATE(Lookupvalue As Long, LookupRange As Range, TableRange As Range, ColumnNum As Integer) As Variant
Dim NameCombine As String
Dim Cnt As Integer
Dim x As Integer
Dim NameString() As String
'Dim b As Boolean
Cnt = WorksheetFunction.CountIfs(LookupRange, Lookupvalue)
If WorksheetFunction.CountIfs(LookupRange, Lookupvalue) > 1 Then
NameCombine = WorksheetFunction.Index(TableRange, WorksheetFunction.Match(Lookupvalue, LookupRange, 0), ColumnNum)
For x = 2 To Cnt
NameCombine = NameCombine & ", " & WorksheetFunction.Index(TableRange, WorksheetFunction.Match(Lookupvalue, LookupRange, 0) + x - 1, ColumnNum)
Next x
NameString() = Split(NameCombine)
MLOOKUPCONCATENATE = RemoveDuplicates(NameString())
Else
NameCombine = WorksheetFunction.Index(TableRange, WorksheetFunction.Match(Lookupvalue, LookupRange, 0), ColumnNum)
MLOOKUPCONCATENATE = NameCombine
End If
End Function
Option 1
Function RemoveDupesDict(Inputarray As Variant) As Variant
Dim i As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For i = LBound(Inputarray) To UBound(Inputarray)
If IsMissing(Replace(Inputarray(i), ",", "")) = False Then
.Item(Replace(Inputarray(i), ",", "")) = 1
End If
Next
RemoveDupesDict = .Keys
End With
End Function
Option 2
Function RemoveDuplicates(ByVal myArray As Variant) As Variant
Dim d As Object
Dim v As Variant
Dim outputArray() As Variant
Dim i As Integer
Set d = CreateObject("Scripting.Dictionary")
For i = LBound(myArray) To UBound(myArray)
d(myArray(i)) = 1
Next i
i = 0
For Each v In d.Keys()
ReDim Preserve outputArray(0 To i)
outputArray(i) = v
i = i + 1
Next v
RemoveDuplicates = outputArray
End Function
Option 3
Function DeDupeString(ByVal sInput As String, Optional ByVal sDelimiter As String = ",") As String
Dim varSection As Variant
Dim sTemp As String
For Each varSection In Split(sInput, sDelimiter)
If InStr(1, sDelimiter & sTemp & sDelimiter, sDelimiter & varSection & sDelimiter, vbTextCompare) = 0 Then
sTemp = sTemp & sDelimiter & varSection
End If
Next varSection
DeDupeString = Mid(sTemp, Len(sDelimiter) + 1)
End Function
# | Group | Date | Name | Amount |
1 | 10 | 2/27/2020 | Johnson | $ 9.00 |
2 | 10 | 2/27/2020 | Johnson | $ 47.00 |
3 | 10 | 2/27/2020 | Johnson | $ 52.00 |
4 | 10 | 2/27/2020 | Johnson | $ 31.00 |
5 | 10 | 3/2/2020 | Blake | $ 56.00 |
6 | 10 | 3/3/2020 | Blake | $ 38.00 |
7 | 10 | 3/3/2020 | Anderson | $ 88.00 |
8 | 15 | 3/5/2020 | Anderson | $ 80.00 |
9 | 15 | 3/6/2020 | Anderson | $ 32.00 |
*Notes on the variables
Lookupvalue = the date you are initially looking for
LookupRange = the column range where the dates are in
TableRange = the entire table array
ColumnNum = the column # within the TableRange that has the name that you want to concatenate
Function MLOOKUPCONCATENATE(Lookupvalue As Long, LookupRange As Range, TableRange As Range, ColumnNum As Integer) As Variant
Dim NameCombine As String
Dim Cnt As Integer
Dim x As Integer
Dim NameString() As String
'Dim b As Boolean
Cnt = WorksheetFunction.CountIfs(LookupRange, Lookupvalue)
If WorksheetFunction.CountIfs(LookupRange, Lookupvalue) > 1 Then
NameCombine = WorksheetFunction.Index(TableRange, WorksheetFunction.Match(Lookupvalue, LookupRange, 0), ColumnNum)
For x = 2 To Cnt
NameCombine = NameCombine & ", " & WorksheetFunction.Index(TableRange, WorksheetFunction.Match(Lookupvalue, LookupRange, 0) + x - 1, ColumnNum)
Next x
NameString() = Split(NameCombine)
MLOOKUPCONCATENATE = RemoveDuplicates(NameString())
Else
NameCombine = WorksheetFunction.Index(TableRange, WorksheetFunction.Match(Lookupvalue, LookupRange, 0), ColumnNum)
MLOOKUPCONCATENATE = NameCombine
End If
End Function
Option 1
Function RemoveDupesDict(Inputarray As Variant) As Variant
Dim i As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For i = LBound(Inputarray) To UBound(Inputarray)
If IsMissing(Replace(Inputarray(i), ",", "")) = False Then
.Item(Replace(Inputarray(i), ",", "")) = 1
End If
Next
RemoveDupesDict = .Keys
End With
End Function
Option 2
Function RemoveDuplicates(ByVal myArray As Variant) As Variant
Dim d As Object
Dim v As Variant
Dim outputArray() As Variant
Dim i As Integer
Set d = CreateObject("Scripting.Dictionary")
For i = LBound(myArray) To UBound(myArray)
d(myArray(i)) = 1
Next i
i = 0
For Each v In d.Keys()
ReDim Preserve outputArray(0 To i)
outputArray(i) = v
i = i + 1
Next v
RemoveDuplicates = outputArray
End Function
Option 3
Function DeDupeString(ByVal sInput As String, Optional ByVal sDelimiter As String = ",") As String
Dim varSection As Variant
Dim sTemp As String
For Each varSection In Split(sInput, sDelimiter)
If InStr(1, sDelimiter & sTemp & sDelimiter, sDelimiter & varSection & sDelimiter, vbTextCompare) = 0 Then
sTemp = sTemp & sDelimiter & varSection
End If
Next varSection
DeDupeString = Mid(sTemp, Len(sDelimiter) + 1)
End Function