+ Reply to Thread
Results 1 to 2 of 2

Remove duplicate values in a dynamic list

  1. #1
    Registered User
    Join Date
    04-03-2014
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2013
    Posts
    2

    Remove duplicate values in a dynamic list

    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
    Last edited by pedrosky1061012; 04-03-2014 at 11:20 AM.

  2. #2
    Registered User
    Join Date
    04-03-2014
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: Remove duplicate values in a dynamic list

    Well i've already got a solution to remove the duplicates value.

    I'll share these code, it may help someone.

    Peace

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Columns("C:C")) Is Nothing Then Exit Sub

    Dim lRow As Integer, lRow_1 As Integer, lRow_2 As Integer
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim MyStringVar1 As Variant
    Dim MyStringVar2 As Variant
    Dim rng As Range, rng1 As Range
    Dim Cell As Range, Cell1 As Range
    Dim cUnique As Collection, cUnique1 As Collection
    Dim g As Double, g1 As Double

    Set ws1 = Worksheets("Dados do Robot")
    Set ws2 = Worksheets("Diagramas de Esforços")

    lRow = ws1.Range("A" & Rows.Count).End(xlUp).Row


    Set rng = ws1.Range("A13:A" & lRow)

    Set cUnique = New Collection

    On Error Resume Next
    For Each Cell In rng.Cells
    cUnique.Add Cell.Value, CStr(Cell.Value)
    Next Cell

    '~~> Create a list for the DV List
    For g = 1 To cUnique.Count
    TempList = TempList & "," & cUnique(g)
    Next

    TempList = Mid(TempList, 2)



    '~~> Create the DV List
    If Len(Trim(TempList)) <> 0 Then
    With ws2.Range("C5").Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=TempList
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
    End With
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trying to Remove a Duplicate in List
    By Getalinks in forum Excel General
    Replies: 6
    Last Post: 09-05-2013, 02:27 AM
  2. Excel 2007 : Remove Duplicate Values from top 5
    By loknath in forum Excel General
    Replies: 1
    Last Post: 11-08-2011, 09:46 AM
  3. Remove Duplicate Entries in List
    By nofzinger in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2010, 06:43 PM
  4. How do I remove duplicate emails from a list?
    By fastrack1 in forum Excel General
    Replies: 2
    Last Post: 01-21-2010, 10:31 PM
  5. How do I remove duplicate entries from a list?
    By E. Reta in forum Excel General
    Replies: 1
    Last Post: 08-24-2005, 02:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1