XL: Sample Macro to Find Duplicate Entries in a Column

ID: Q142591


The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for Windows, versions 5.0, 5.0c


SUMMARY

The Microsoft Visual Basic for Applications sample macro in this article finds duplicate cell entries within a column and changes the color of the cell containing the duplicate entry to red.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
In order for the macro to work correctly, make sure the following conditions are met:
  • The column in which you want to find duplicates must be sorted based on values in that column.


  • -and-

  • The first cell in the column in which you want to find duplicates must be selected.


Sample Visual Basic Code


   Sub FindDups ()
      ScreenUpdating = False
      FirstItem = ActiveCell.Value
      SecondItem = ActiveCell.Offset(1, 0).Value
      Offsetcount = 1
      Do While ActiveCell <> ""
         If FirstItem = SecondItem Then
           ActiveCell.Offset(Offsetcount,0).Interior.Color = RGB(255,0,0)
           Offsetcount = Offsetcount + 1
           SecondItem = ActiveCell.Offset(Offsetcount, 0).Value
         Else
           ActiveCell.Offset(Offsetcount, 0).Select
           FirstItem = ActiveCell.Value
           SecondItem = ActiveCell.Offset(1,0).Value
           Offsetcount = 1
         End If
      Loop
      ScreenUpdating = True
   End Sub 

Additional query words: 5.00c 8.00 xl97 double repeat

Keywords : kbprg kbdta kbdtacode KbVBA kbhowto
Version : WINDOWS:5.0,5.0c,7.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.