Results 1 to 2 of 2

VBA Macro to find Duplicates using Ranges

Threaded View

  1. #1
    Registered User
    Join Date
    08-12-2012
    Location
    Noumea
    MS-Off Ver
    Excel 2003
    Posts
    1

    VBA Macro to find Duplicates using Ranges

    Hi

    VBA noob here, been searching for 2 days to find a script i can modify for my needs but keep getting stuck or not be able to make anything work for my specific situation.

    I'm trying to write a simple but specific macro to find and color duplicates in ranges.

    My search criteria is in Range(B5:B405) Data to be scanned and colored is located in Range(D5:OM1004)

    The data is only numbers and needs to be an exact match to the search criteria, if cell in data is found to exist in search criteria then data cell is filled red.

    I also need to stop the script at data row 1004 and display a message with total execution time at the end.

    I can do this in seconds with Conditional Formatting but I need to count the colored cells after and no VBA Macros i can find will let me count conditionally formatted colors, even been through all of cpearson's site without success.


    Working Code is here, i hope this helps someone in the future.

    Option Explicit
    Sub ColorCriteria()
        Dim rCriteria As Range
        Dim rData As Range
        Dim c As Range, r As Range
        Dim sFirstAddress As String
        Dim ColorCounter As Long
        Dim StartTime As Single, EndTime As Single
    
    StartTime = Timer
    Set rCriteria = Range("B5:B405")
    Set rData = Range("D5:OM1004")
    
    Application.ScreenUpdating = False
    With rData
        .Interior.ColorIndex = xlNone
    
    For Each r In rCriteria
        If Not r = "" Then
        Set c = .Find(what:=r.Value, LookIn:=xlValues, lookat:=xlWhole, _
                searchdirection:=xlNext)
        If Not c Is Nothing Then
            sFirstAddress = c.Address
            c.Interior.Color = vbRed
    
            Do
                Set c = .FindNext(c)
                c.Interior.Color = vbRed
                ColorCounter = ColorCounter + 1
            Loop Until c.Address = sFirstAddress
        End If
        End If
    Next r
    
    End With
    Application.ScreenUpdating = True
    EndTime = Timer
    
    MsgBox ("Execution Time: " & Format(EndTime - StartTime, "0.000"" sec""") _
        & vbLf & "Colored Cell Count: " & ColorCounter)
    
    
    End Sub
    Last edited by Kazper; 12-08-2013 at 12:17 AM. Reason: Solved

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to find duplicates, concatenate cells, then delete old duplicates
    By givemepuppies in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 03-04-2016, 02:43 AM
  2. [SOLVED] Macro to find duplicates, concatenate Unique Values, then delete old duplicates
    By lesoies in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2013, 04:32 PM
  3. [SOLVED] Pivot Table Macro Error and Duplicates for varying ranges
    By dwhite30518 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-17-2012, 11:26 AM
  4. macro to find duplicates in 1 column
    By vsantoro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2011, 05:20 PM
  5. Find and delete duplicates (macro)
    By famico78 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2009, 09:03 PM

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