Results 1 to 5 of 5

Thread: List duplicates in Excel

  1. #1

    Thread Starter
    Junior Member jmcgill's Avatar
    Join Date
    Jul 2005
    Posts
    22

    Question List duplicates in Excel

    I am needing to create a list in Excel from one column in sheet 1 that will make up the list in a different column. The problem I have is that the 1st column(that will populate the list) may have the same entry in it several times along with several distinct entries. I dont wont to show duplicates or any blank cells in the list box. In VB I would just write a Select Distinct statement but in Excel I am not sure how this is done.

    Can some one point me in the right direction?

    Thanks,

    Jason

  2. #2
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: List duplicates in Excel

    Wouldn't know how to write the complete code, but I would do this using an array.

    1. Add the value of the first cell in your source range to your array.
    2. Check if the value of the second cell is the same as the first entry in the array. If it is not increase the array size by one (ReDim Preserve) and add the cells value. If it is the same, ignore it.
    3. Repeat step 2 for all other cells in your source range, but each time checking the value of the cell against all entries in the array. If the cells value already exists do nothing. If it doesn't exist redim and add.
    4. Add your array to the required column (each individual entry being added to its own cell).


    Hope this makes sense.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  3. #3

    Thread Starter
    Junior Member jmcgill's Avatar
    Join Date
    Jul 2005
    Posts
    22

    Re: List duplicates in Excel

    it makes sense but i do not know much about vba in excel. any idea where i can get an example?

  4. #4
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: List duplicates in Excel

    Replaced by next post

    EDIT:

    Don't know if it helps, but you can actually find duplicates without vba. See http://office.microsoft.com/en-us/as...366161033.aspx
    Last edited by New2vba; May 3rd, 2006 at 12:40 PM. Reason: Superseded
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  5. #5
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: List duplicates in Excel

    This one really played on my mind so I had another attempt and think I've managed it

    Try this code:

    VB Code:
    1. 'Change the default index of the first element in the array from 0 to 1
    2. Option Base 1
    3.  
    4. Sub CopyList()
    5.  
    6. Dim NewList() As String
    7. Dim shtSource As Worksheet
    8. Dim shtTarget As Worksheet
    9. Dim i As Long
    10. Dim j As Long
    11. Dim k As Long
    12. Dim Flag As Long
    13.  
    14. 'Set source and target worksheets
    15. Set shtSource = ActiveWorkbook.Worksheets("Sheet1")
    16. Set shtTarget = ActiveWorkbook.Worksheets("Sheet2")
    17.  
    18. 'Set flag to zero
    19. Flag = 0
    20.  
    21. 'Redim array and add first list item from A1
    22. ReDim NewList(1) As String
    23. NewList(1) = shtSource.Cells(1, 1)
    24.  
    25.  
    26. 'Assuming a source range of A1:A9 (A1 has already been added to the list)
    27. For i = 2 To 9
    28.     For j = LBound(NewList) To UBound(NewList)
    29.         'Check if i'th source cell is same as j'th value in array
    30.         If shtSource.Cells(i, 1) <> NewList(j) Then
    31.             'If item is unique add flag
    32.             Flag = Flag + 1
    33.         End If
    34.     Next j
    35.         'If every check flagged then item is unique
    36.         If Flag = UBound(NewList) Then
    37.             'Redim array and add unique value
    38.             ReDim Preserve NewList(UBound(NewList) + 1)
    39.             NewList(UBound(NewList)) = shtSource.Cells(i, 1)
    40.             'Reset flag
    41.             Flag = 0
    42.         Else
    43.             'Reset flag
    44.             Flag = 0
    45.         End If
    46. Next i
    47.  
    48. 'Add the completed array to the target range
    49. For k = LBound(NewList) To UBound(NewList)
    50.     shtTarget.Cells(k, 1) = NewList(k)
    51. Next k
    52.  
    53. End Sub

    I've tried it with several different ranges and it seems to work fine. I've also attached a sample spreadsheet with the working code.

    Let me know if it solves your problem.
    Attached Files Attached Files
    Last edited by New2vba; May 3rd, 2006 at 12:44 PM. Reason: Added file
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

Posting Permissions

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



Click Here to Expand Forum to Full Width