Hi,
I want to remove the duplicate value in the list, but not delete the value nor the row where the value is originated.
The list is made from one table in one worksheet and the list is on a different worksheet .
I have this code and is not working, could someone tell me what's wrong with it.
Thank you
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Columns("C:C")) Is Nothing Then Exit Sub
Dim lRow As Integer
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Dados do Robot")
Set ws2 = Worksheets("Diagramas de Esforços")
lRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
ws1.Range("A13:A" & lRow).Name = "MyList"
lRow = ws1.Range("C" & Rows.Count).End(xlUp).Row
ws1.Range("C13:C" & lRow).Name = "MyList_1"
Call RemoveDuplicates
End Sub
Sub RemoveDuplicates()
Dim rgInput As Range
Dim rgOutput As Range
Dim avOutput As Variant
Dim a As Long
Dim lRow As Integer
Dim ws1 As Worksheet
Set ws1 = Worksheets("Dados do Robot")
lRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
Set rgInput = ws1.Range("A13:A" & lRow)
avOutput() = UniqueItems(rg, False)
For a = 0 To UBound(avOutput)
Sheets("OUTPUTSHEET").Range("A1").Offset(a, 0).Value = avOutput(a)
Next a
Set rgOutput = Sheets("OUTPUTSHEET").Range("A1", Cells(UBound(avOutput), 1))
ActiveWorkbook.Names.Add Name:="Themes", RefersTo:=rgOutput
End Sub
Function UniqueItems(ArrayIn As Variant, Optional Count As Boolean = True) As Variant
' Accepts an array or range as input
' If Count = True or is missing, the function returns the number of unique elements
' If Count = False, the function returns a variant array of unique elements
Dim Unique() As Variant ' array that holds the unique items
Dim Element As Variant
Dim i As Integer
Dim FoundMatch As Boolean
Dim NumUnique As Long
' Counter for number of unique elements
NumUnique = 0
' Loop thru the input array
For Each Element In ArrayIn
FoundMatch = False
' Has item been added yet?
For i = 1 To NumUnique
If Element = Unique(i) Then
FoundMatch = True
GoTo AddItem '(Exit For-Next loop)
End If
Next i
AddItem:
' If not in list, add the item to unique list
If Not FoundMatch Then
NumUnique = NumUnique + 1
ReDim Preserve Unique(NumUnique)
Unique(NumUnique) = Element
End If
Next Element
' Assign a value to the function
If Count Then UniqueItems = NumUnique Else UniqueItems = Unique
End Function
Bookmarks