Consulting

Results 1 to 6 of 6

Thread: find duplicates

  1. #1
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    2
    Location

    find duplicates

    hi all,
    new to this, i am trying to write a macro loop through the column and color all duplicates of cells. i first sort the column then i want it to loop through and color all the duplicate(except the first one) in red. that is the code i have so far it does not seems to work well.

    Sub Macro1()
    Dim Count As Integer
    Dim Previous As Integer
    Dim range As Integer
    Dim curCell As Integer
    Dim nextCell As Integer
    Dim lastCell As Integer

    curCell = 1
    nextCell = 2
    lastCell = 3

    Count = 2
    range = 10
    Previous = Count - 1

    Do While Count < range
    If StrComp(Cells(Previous, curCell), Cells(Count, curCell), vbTextCompare) = 0 Then
    Cells(Count, curCell + 1).Interior.Color = RGB(255, 0, 0)

    Else:

    Cells(Previous, curCell + 1).Interior.Color = RGB(255, 67, 0)

    End If
    Count = Count + 1
    Loop
    End Sub


    Thanks alot, Oamram.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Moved to the appropriate forum.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Code results seem odd, but is this what you want?

    [vba]

    Sub Macro1()
    Dim Count As Integer
    Dim Previous As Integer
    Dim range As Integer
    Dim curCell As Integer
    Dim nextCell As Integer
    Dim lastCell As Integer

    curCell = 1
    nextCell = 2
    lastCell = 3

    Count = 2
    range = 10

    Do While Count < range
    Previous = Count - 1
    If StrComp(Cells(Previous, curCell), Cells(Count, curCell), vbTextCompare) = 0 Then

    Cells(Count, curCell + 1).Interior.Color = RGB(255, 0, 0)
    Else

    Cells(Previous, curCell + 1).Interior.Color = RGB(255, 67, 0)
    End If
    Count = Count + 1
    Loop
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    2
    Location
    Thanks man, it works, do you mind giving a quick explanation on the changes you made.

    Thanks, oamram.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have a look at this
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I just moved the line

    [vba]

    Previous = Count - 1
    [/vba]

    from outside of the Do Loop, to inside at the start.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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