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