Results 1 to 4 of 4

Thread: Check Duplicates - Speed it up.

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Post Check Duplicates - Speed it up.

    I had a requirement to check for the presence of duplicates in a column in Excel.
    I have written a function for this purpose and it works. The drawback is that it is an extremely slow function and takes about 6 minutes to check across 20000 rows. Could someone take a look and speed up the function?


    VB Code:
    1. Sub CheckDuplicate()
    2.     Dim txtSearchVal As String
    3.     Dim lCtr As Long
    4.     Dim lCtr2 As Long
    5.     Dim bFound As Boolean
    6.    
    7. For lCtr2 = 1 To 20000
    8.     txtSearchVal = Sheet1.Cells(lCtr2, 1)
    9.     For lCtr = (lCtr2 + 1) To 20000
    10.         If Sheet1.Cells(lCtr, 1) = txtSearchVal Then
    11.             Sheet1.Cells(lCtr, 1).Font.Color = vbRed
    12.             Sheet1.Cells(lCtr, 1).Font.Bold = True
    13.             'Sheet1.Cells(lCtr, 1).AddComment ("Duplicate Values")
    14.             Exit For
    15.         End If
    16.     Next lCtr
    17. Next lCtr2
    18. End Sub
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Check Duplicates - Speed it up.

    Do you have data all the way to the 20000th row?

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Check Duplicates - Speed it up.

    Yes. We do.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4
    Member
    Join Date
    Jun 2005
    Posts
    41

    Re: Check Duplicates - Speed it up.

    I have used this page a few times to straighten me out in my coding endeavors:
    http://www.ozgrid.com/VBA/VBALoops.htm

    Here is what I think might interest you, modified slightly to your needs:
    VB Code:
    1. Sub Dupes()
    2. Dim DupeValue As Double
    3. Dim ii As Integer
    4. DupeValue = 2 'whatever
    5.     If WorksheetFunction.CountIf(Cells, DupeValue) = 0 Then
    6.        MsgBox "No duplicate values."
    7.        Exit Sub
    8.     End If
    9.     Range("A1").Select
    10.     For ii = 1 To WorksheetFunction.CountIf(Cells, DupeValue)
    11.         Cells.Find(What:=2, After:=ActiveCell, LookIn:=xlFormulas, _
    12.         LookAt:=xlPart, SearchOrder:=xlByRows, _
    13.         SearchDirection:=xlNext, MatchCase:=False).Activate
    14.         ActiveCell.Interior.ColorIndex = 4 'green highlight
    15.     Next ii
    16. End Sub

    This still uses a loop, and perhaps someone else knows the way to get the cells.find function to find ALL the values at once, and not just the first one. I looked, but I didn't find any resources regarding that.
    This loop's running time will directly depend on the number of duplicates in the sheet. If its only a few hundred, it shouldn't take too long to run at all, even on this old Pentium 166.

    Hope this helps you 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