+ Reply to Thread
Results 1 to 5 of 5

VBA Macro checks for duplicates,works great, but wish to add a select row

  1. #1
    Registered User
    Join Date
    12-30-2011
    Location
    East, Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    4

    VBA Macro checks for duplicates,works great, but wish to add a select row

    Hi,

    I've found this vba script that locates duplicate values in a excelsheet.
    If is has found some duplicate values, it gives these cells a certain color.
    If it finds a single rows without a duplicate, it deletes this row.
    It works like a charm, but i would like to have a modification on this script.

    It would be great to include a option, so that it only checks 1 column (B) for duplicate values.
    I tried to include this line:

    Columns("B:B").Select

    But that does not work. Is there anyone who can give me a solution, or at least a kick in the right direction ?

    Script:
    ================================================================================

    Public Sub HighlightDuplicateRows()
    '
    ' This macro highlights duplicate rows in the selection and deletes
    ' unique rows. Duplicates are counted in the COLUMN of the active cell.

    Dim r As Long
    Dim C As Range
    Dim V As Variant
    Dim Rng As Range
    Dim Color As Integer

    On Error GoTo EndMacro
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    If Selection.Rows.Count > 1 Then
    Set Rng = Selection
    Else
    Set Rng = ActiveSheet.UsedRange.Rows
    End If
    Color = 44
    For r = Rng.Rows.Count To 1 Step -1
    V = Rng.Cells(r, 1).Value
    If V <> V1 Then
    Color = Color - 2
    If Color = 34 Then Color = 44
    End If
    V1 = V
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then
    Rng.Rows(r).EntireRow.Select
    With Selection.Interior
    .ColorIndex = Color
    .Pattern = xlSolid
    End With
    Else
    Rng.Rows(r).EntireRow.Delete
    End If
    Next r
    EndMacro:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub

    =====================================================================================

    Thanks in advance (a lot !!)
    Mesjoggah

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Macro checks for duplicates,works great, but wish to add a select row

    Hey welcome to the forum. It would be good if you put your code in code tags. You can check the forum rules for some guidance on that.

    Change this line from
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA Macro checks for duplicates,works great, but wish to add a select row

    @arlu1201
    The line
    Please Login or Register  to view this content.
    will give 65536 in Excel 2003, 1 million+ in Excel 2007 etc. What does this line check then?

  4. #4
    Registered User
    Join Date
    12-30-2011
    Location
    East, Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA Macro checks for duplicates,works great, but wish to add a select row

    Thanks a lot !
    Works perfect !
    Thank

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Macro checks for duplicates,works great, but wish to add a select row

    The user had used "Selection.rows.count" so i got it changed to refer the columns that the user wanted. I guess after the next post (post 4) it worked for the user.
    Even though its not the fool proof way of getting it to work, but its always going to run because 65536 will always be >1.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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