Need some help modifying the below code. I need the code to also remove blanks. It currently removes duplicates correctly, however I also need it to remove blanks from the source. Any help is much appreciated
http://www.mrexcel.com/forum/images/smilies/icon_smile.gif
http://www.mrexcel.com/forum/images/smilies/icon_smile.gif
Code:
Dim AllCells As Range, cell As Range
Dim NoDupes As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
' The items are in Table Industry Code to Buyer Matchup[Key Desc]field
Set AllCells = Range("Table_Industry_Code_to_Buyer_Matchup[Key Desc]")
' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
For Each cell In AllCells
NoDupes.Add cell.Value, CStr(cell.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next cell
' Resume normal error handling
On Error GoTo 0
' Sort the collection (optional)
For i = 1 To NoDupes.Count - 1
For j = i + 1 To NoDupes.Count
If NoDupes(i) > NoDupes(j) Then
Swap1 = NoDupes(i)
Swap2 = NoDupes(j)
NoDupes.Add Swap1, before:=j
NoDupes.Add Swap2, before:=i
NoDupes.Remove i + 1
NoDupes.Remove j + 1
End If
Next j
Next i
' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
NonIDBuySupplier.IndCode.AddItem Item
Next Item