+ Reply to Thread
Results 1 to 14 of 14

Find & highlight rows using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2013
    Location
    India
    MS-Off Ver
    2013
    Posts
    56

    Find & highlight rows using VBA

    Hi Experts,

    I need to highlight duplicate rows based on 3 columns using VBA.

    This is what I am looking for.

    Compare the values in column B, F & H.
    Highlight the duplicate values in column F & H. Also highlight the newest value in "Green" in column L & M based on the date in column B. I've attached a sample spreadsheet.

    I tried using the conditional formatting with formula but my spreadsheet is freezing and lands in to recovery mode. The file has more than 10000 rows.
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Find & highlight rows using VBA

    Hi Max_excel,

    Try below code based on your sample file ...

    Sub test()
    
    Dim Lc&, a, j$
    Lc = Cells(1, Columns.Count).End(1).Column + 1
    a = Cells(1).CurrentRegion
    
    With CreateObject("scripting.dictionary")
       For x = 2 To UBound(a)
          j = Join(Array(a(x, 6), a(x, 8)), "-")
          If Not .exists(j) Then
             .Add j, 1 & "|" & a(x, 2)
          Else
             .Item(j) = Split(.Item(j), "|")(0) + 1 & "|" & IIf(Split(.Item(j), "|")(1) < a(x, 2), Split(.Item(j), "|")(1), a(x, 2))
          End If
       Next
       For x = 2 To UBound(a)
          j = Join(Array(a(x, 6), a(x, 8)), "-")
          a(x, 3) = IIf(Split(.Item(j), "|")(0) > 1, Split(.Item(j), "|")(0), "-")
          a(x, 4) = IIf(Split(.Item(j), "|")(0) > 1, Split(.Item(j), "|")(1), "-")
       Next
       Cells(1, Lc).Resize(UBound(a), 2) = Application.Index(a, Evaluate("row(1:" & UBound(a) & ")"), Array(3, 4))
    End With
    
    For x = 2 To UBound(a)
       If Cells(x, Lc) <> "-" Then
          Union(Cells(x, "F"), Cells(x, "H")).Interior.Color = vbRed
          If Cells(x, "B") = Cells(x, Lc).Offset(, 1) Then Union(Cells(x, "L"), Cells(x, "O")).Interior.Color = 5296274
       End If
    Next
    
    Columns(Lc).Resize(, 2).ClearContents
    
    End Sub
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Registered User
    Join Date
    01-30-2013
    Location
    India
    MS-Off Ver
    2013
    Posts
    56

    Re: Find & highlight rows using VBA

    Hi nankw8,

    Thanks for the code it works like charm.

    However, I missed to inform that Rows 1 & 2 are merged as these are the headers, these rows doesn't require to highlight duplicates. Also, column O has formula and is being removed when I run this code.

  4. #4
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Find & highlight rows using VBA

    Can you post a sample file that mimics your original file along with the merge cells so I can adjust the code accordingly

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Find & highlight rows using VBA

    Another Option...
    Sub sintek()
    Dim Data, X, i As Long, rw As Long
    Application.ScreenUpdating = False
    With Sheets("Sheet1").Cells(1).CurrentRegion
        Data = .Value
        For i = 2 To UBound(Data)
            rw = Evaluate("=MAX(if(" & .Columns(6).Address & "=""" & Data(i, 6) & """,IF(" & .Columns(8).Address & "=" & Data(i, 8) & ",ROW(" & .Columns(2).Address & "))))")
            X = Evaluate("=COUNTIFS(" & .Columns(6).Address & ",""" & Data(i, 6) & """, " & .Columns(8).Address & ", """ & Data(i, 8) & """)")
            If X > 1 Then With .Parent: Union(Range("F" & i), .Range("H" & i)).Interior.Color = vbRed: Union(.Range("L" & rw), .Range("O" & rw)).Interior.Color = vbGreen: End With
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  6. #6
    Registered User
    Join Date
    01-30-2013
    Location
    India
    MS-Off Ver
    2013
    Posts
    56

    Re: Find & highlight rows using VBA

    Sure, I've attached the sample file "Sample_Find Duplicate"
    Attached Files Attached Files

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Find & highlight rows using VBA

    For my code just change 2 to 3

    For i = 2 To UBound(Data)

    PS...Are you guys also experiencing issues with the Forums Layout & View....
    Untitled.png
    Last edited by Sintek; 08-26-2020 at 06:26 AM.

  8. #8
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Find & highlight rows using VBA

    Quote Originally Posted by sintek View Post
    PS...Are you guys also experiencing issues with the Forums Layout & View....
    Yes, I am having the same issue

  9. #9
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Find & highlight rows using VBA

    Code revised ...

    Sub test()
    
    Dim Lc&, a, j$
    Lc = Cells(2, Columns.Count).End(1).Column + 1
    a = Cells(1).CurrentRegion.Offset(1)
    
    With CreateObject("scripting.dictionary")
       For x = 2 To UBound(a)
          j = Join(Array(a(x, 6), a(x, 8)), "-")
          If Not .exists(j) Then
             .Add j, 1 & "|" & a(x, 2)
          Else
             .Item(j) = Split(.Item(j), "|")(0) + 1 & "|" & IIf(Split(.Item(j), "|")(1) < a(x, 2), Split(.Item(j), "|")(1), a(x, 2))
          End If
       Next
       For x = 2 To UBound(a)
          j = Join(Array(a(x, 6), a(x, 8)), "-")
          a(x, 3) = IIf(Split(.Item(j), "|")(0) > 1, Split(.Item(j), "|")(0), "-")
          a(x, 4) = IIf(Split(.Item(j), "|")(0) > 1, Split(.Item(j), "|")(1), "-")
       Next
       Cells(2, Lc).Resize(UBound(a), 2) = Application.Index(a, Evaluate("row(1:" & UBound(a) & ")"), Array(3, 4))
    End With
    
    For x = 3 To UBound(a)
       If Cells(x, Lc) <> "-" Then
          Union(Cells(x, "F"), Cells(x, "H")).Interior.Color = vbRed
          If Cells(x, "B") = Cells(x, Lc).Offset(, 1) Then Union(Cells(x, "L"), Cells(x, "O")).Interior.Color = 5296274
       End If
    Next
    
    Columns(Lc).Resize(, 2).ClearContents
    
    End Sub

  10. #10
    Registered User
    Join Date
    01-30-2013
    Location
    India
    MS-Off Ver
    2013
    Posts
    56

    Re: Find & highlight rows using VBA

    Thanks so much! The last version of code does the job.

  11. #11
    Registered User
    Join Date
    01-30-2013
    Location
    India
    MS-Off Ver
    2013
    Posts
    56

    Re: Find & highlight rows using VBA

    I tried your option 2 and it worked on my sample sheet. However, I get Run Time error - 1004 Application-defined or object-defined error when pasted this code on my master report. I've attached the screenshot.

    Forum page is loading perfectly for me
    Attached Images Attached Images

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Find & highlight rows using VBA

    My bad...missing a Point see red dot

    If X > 1 Then With .Parent: Union(.Range("F" & i), .Range("H" & i)).Interior.Color = vbRed: Union(.Range("L" & rw), .Range("O" & rw)).Interior.Color = vbGreen: End With
    Last edited by Sintek; 08-26-2020 at 08:30 AM.

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Find & highlight rows using VBA

    Glad you got it sorted...tx for rep +

  14. #14
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: Find & highlight rows using VBA

    Glad to help & thanks Max_excel for the reps

+ 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. Find All and highlight selected rows
    By amberle in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-06-2020, 08:45 AM
  2. Problem with macro that needs to find and highlight rows based on searching cells
    By mnk181 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2014, 12:24 PM
  3. Replies: 1
    Last Post: 02-17-2014, 03:17 PM
  4. Quick way to find and highlight multiple rows?
    By tnovak in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 02-14-2014, 01:09 PM
  5. Replies: 0
    Last Post: 11-12-2013, 04:56 PM
  6. Replies: 2
    Last Post: 06-27-2012, 12:27 PM
  7. [SOLVED] highlight/remove highlight of rows conditional on cellvalue
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-01-2012, 07:14 AM

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