+ Reply to Thread
Results 1 to 9 of 9

VBA to Highlight Duplicate Cells and Show Msg Box

  1. #1
    Registered User
    Join Date
    02-08-2021
    Location
    Michigan, USA
    MS-Off Ver
    O365
    Posts
    54

    VBA to Highlight Duplicate Cells and Show Msg Box

    I am trying to write a VBA code to highlight duplicate cells and show message box. The code I have below works but when a duplicate is entered only the first duplicate cell is highlighting in red and the message box shows. I want both cells to highlight red and show the message box. Any help on this is greatly appreciated.

    Private Sub Worksheet_Change(ByVal target As Range)

    Dim myRange As Range
    Dim i As Integer
    Dim j As Integer
    Dim myCell As Range

    Set myRange = Range("D1:D10")

    For Each myCell In myRange
    If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
    myCell.Interior.ColorIndex = 3
    GoTo DisplayMsg

    ElseIf WorksheetFunction.CountIf(myRange, myCell.Value) = 1 Or WorksheetFunction.CountIf(myRange, myCell.Value) = 0 Then
    myCell.Interior.ColorIndex = 0

    End If

    Exit Sub

    Next

    DisplayMsg:
    MsgBox "Duplicate URL's entered are highlighted in red"


    End Sub
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: VBA to Highlight Duplicate Cells and Show Msg Box

    You have a goto line when a duplicate has been found and you also exit the sub before next so the loop will stop

  3. #3
    Registered User
    Join Date
    02-08-2021
    Location
    Michigan, USA
    MS-Off Ver
    O365
    Posts
    54

    Re: VBA to Highlight Duplicate Cells and Show Msg Box

    Thank you for the response davesexcel. Are you or someone else able to advise what the code should be for this type of situation. I have tried removing the GoTo, the Next, and moving the display message and am not able to get the result I am looking for stated above in original message.

    Thank you for the help, everyone.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    So removing the useless the worksheet event VBA procedure revamped :

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
    With [D1:D10]
            If 
    Intersect(.CellsTargetIs Nothing Then Exit Sub
           
    .Interior.ColorIndex xlNone
            V 
    Join(Filter([TRANSPOSE(IF(COUNTIF(D1:D10,D1:D10)>1,"D"&ROW(D1:D10),FALSE))], FalseFalse), ",")
            If 
    "" Then Range(V).Interior.ColorIndex 6
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb or this ...


    As here few range cells so slow looping is not such a concern like with this Xmas tree variation :

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
    Dim CK%, R%, Rf As Range
       With 
    [D1:D10]
            If 
    Intersect(.CellsTargetIs Nothing Then Exit Sub
           
    .Interior.ColorIndex xlNone
            C 
    = Array(4682438)
            
    = [RANDBETWEEN(0,4)]
        For 
    1 To 9
         
    If .Cells(R).Interior.ColorIndex xlNone And Not IsEmpty(.Cells(R)) Then
            Set Rf 
    = .Find(.Cells(R), .Cells(R), xlValues1)
             If 
    Rf.Row R Then
                K 
    = (1Mod 5
               
    .Cells(R).Interior.ColorIndex C(K)
             Do
                
    Rf.Interior.ColorIndex C(K)
                
    Set Rf = .FindNext(Rf)
             
    Loop Until Rf.Row R
             End 
    If
         
    End If
        
    Next
       End With
            Set Rf 
    Nothing
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA to Highlight Duplicate Cells and Show Msg Box

    What if you use conditional formatting to highlight the duplicates, then use the change event to give the message.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-08-2021
    Location
    Michigan, USA
    MS-Off Ver
    O365
    Posts
    54

    Re: VBA to Highlight Duplicate Cells and Show Msg Box

    Thank you Marc and ByteMarks for your responses. I have tried all three responses and am not getting exactly what I am looking for. Marc your codes removed the message box which I need. ByteMarks yes, I could use conditional formatting, but I need this built into the code. Thank you for helping out with this.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA to Highlight Duplicate Cells and Show Msg Box


    As I wrote « removing the useless » but you can add it …

  9. #9
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA to Highlight Duplicate Cells and Show Msg Box

    Based on your original code, maybe:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Highlight all duplicate cells ignoring blank cells
    By Vivek2705 in forum Excel General
    Replies: 10
    Last Post: 05-22-2024, 04:54 PM
  2. [SOLVED] Highlight cells that are duplicate pairs
    By erob223 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-22-2015, 10:46 AM
  3. [SOLVED] Find Duplicate - Highlight Cells
    By da80th in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 01-15-2013, 07:49 PM
  4. Highlight cells if duplicate found on the same row
    By martinez_pedro in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-23-2010, 04:24 AM
  5. Highlight Visible Duplicate Cells
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2010, 10:20 AM
  6. Highlight Duplicate Cells in Different Colors
    By dbla921 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-11-2008, 04:43 PM
  7. Highlight duplicate cells in next column...
    By ImpetuousRacer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-25-2008, 03:24 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