I found this file that makes a userform extracting unique values and sorting.
I'm trying to get the additem to add to a cell range instead of the userform.
Here is the origional code slightly modified (works on the form)
[vba]Option Explicit
' This example is based on a tip by J.G. Hussey,
' published in "Visual Basic Programmer's Journal"
Sub RemoveDuplicates()
Dim AllCells As Range, Cell As Range, Cellb As Range
Dim AllCellsb As Range
Dim NoDupes As New Collection
Dim NoDupesb As New Collection
Dim i As Integer, j As Integer
Dim ib As Integer, jb As Integer
Dim Swap1, Swap2, Item
Dim Swap1b, Swap2b, Itemb
' The items are in a range named Countries
Set AllCells = Range("TechName")
Set AllCellsb = Range("TechID")
' 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
For Each Cellb In AllCellsb
NoDupesb.Add Cellb.Value, CStr(Cellb.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cellb
' Resume normal error handling
On Error GoTo 0
' Update the labels on UserForm1
With UserForm1
.Label1.Caption = "Total Techs: " & AllCells.Count
.Label2.Caption = "Unique Techs: " & NoDupes.Count
.Label3.Caption = "Total IDs: " & AllCellsb.Count
.Label4.Caption = "Unique IDs: " & NoDupesb.Count
End With
' 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
For ib = 1 To NoDupesb.Count - 1
For jb = ib + 1 To NoDupesb.Count
If NoDupesb(ib) > NoDupesb(jb) Then
Swap1b = NoDupesb(ib)
Swap2b = NoDupesb(jb)
NoDupesb.Add Swap1b, before:=jb
NoDupesb.Add Swap2b, before:=ib
NoDupesb.Remove ib + 1
NoDupesb.Remove jb + 1
End If
Next jb
Next ib
' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
UserForm1.ListBox1.AddItem Item
Next Item
For Each Itemb In NoDupesb
UserForm1.ListBox2.AddItem Itemb
Next Itemb
' Show the UserForm
UserForm1.Show
End Sub
[/vba]
This is what I have so far.
It stops at this line
Range("B:B").Value = NoDupes
I think the entire collection could just be pasted to b1 and fill down,
Not sure if it needs a loop because the values are allready arranged and sorted, so the entire collection should be able to be just pasted.
I tried the range.value = collection idea, but no go...
Any Ideas?
[vba]Option Explicit
Sub RemoveDuplicates()
Dim AllCells As Range, Cell As Range, Cellb As Range
Dim AllCellsb As Range
Dim NoDupes As New Collection
Dim NoDupesb As New Collection
Dim i As Integer, j As Integer
Dim ib As Integer, jb As Integer
Dim Swap1, Swap2, Item
Dim Swap1b, Swap2b, Itemb
Dim wsDest As Worksheet
' The items are in a range named TechName and Techs
Set AllCells = Worksheets("QCDetail").Range("TechName") ' column A QCDetail
Set AllCellsb = Worksheets("QCDetail").Range("Techs") ' column B QCDetail Sort this
Set wsDest = Worksheets("WQC")
' 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
For Each Cellb In AllCellsb
NoDupesb.Add Cellb.Value, CStr(Cellb.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cellb
' 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
For ib = 1 To NoDupesb.Count - 1
For jb = ib + 1 To NoDupesb.Count
If NoDupesb(ib) > NoDupesb(jb) Then
Swap1b = NoDupesb(ib)
Swap2b = NoDupesb(jb)
NoDupesb.Add Swap1b, before:=jb
NoDupesb.Add Swap2b, before:=ib
NoDupesb.Remove ib + 1
NoDupesb.Remove jb + 1
End If
Next jb
Next ib
' Add the sorted, non-duplicated items to a ListBox
With Sheets("WQC")
.Range("B1:B" & Rows.Count).ClearContents
.Range("C1:C" & Rows.Count).ClearContents
End With
For Each Item In NoDupes ' Tech Name
' UserForm1.ListBox1.AddItem Item
Range("B:B").Value = NoDupes
Next Item
For Each Itemb In NoDupesb ' Tech ID
' UserForm1.ListBox2.AddItem Itemb
Range("C:C").Value = NoDupesb
Next Itemb
End Sub[/vba]