Macro modification needed.

nair.harish

New Member
Joined
Mar 2, 2010
Messages
40
Hi

The below macro works perfectly, except that it does not highlight duplicate lines on a particular sheet. If there are 3 similar lines it highlights only one and moves forward. heres the link to the workbook (http://www57.zippyshare.com/view.jsp?locale=en&key=97096454), i have marked it in red the lines that did not highlight. the consolidated sheet is where the macro refers which lines to highlight.

Sub replaceColours()
Dim cl As Range
Dim CI As Long
Dim i As Integer
Dim strText As String
For Each cl In ActiveSheet.UsedRange.Columns(1).Cells
CI = cl.Interior.Color
If CI <> 16777215 Then
strText = cl.Value
For i = 1 To Worksheets.Count

' find next instance of strText
Dim rngNext As Range: Set rngNext = Worksheets(i).Cells.Find(What:=strText, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)

' if found, colour it
If Not rngNext Is Nothing Then rngNext.Interior.Color = CI

Next i
End If
Next cl
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Nair - .Find method only finds one occurrence. I added a loop to find all occurrences. Here is the modified code. Let me know if it works. Good luck!

Code:
Sub replaceColours()
Dim cl As Range
Dim CI As Long
Dim i As Integer
Dim strText As String
Dim rngNext As Range

With Application
    CurrentScreenUpdating = .ScreenUpdating
    CurrentCalculate = .Calculation
    CurrentEnableEvents = .EnableEvents
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

Sheets("Consolidated").Activate

For Each cl In ActiveSheet.UsedRange.Columns(1).Cells
    CI = cl.Interior.Color
    cl.Select
    If CI <> 16777215 Then
        strText = cl.Value
        For i = 1 To Worksheets.Count
            oldAddress = ""
            flag = True
            Do Until flag = False
            ' find next instance of strText
            Set rngNext = Worksheets(i).Cells.Find(What:=strText, After:=ActiveCell, LookIn:= _
                xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
                xlNext, MatchCase:=False, SearchFormat:=False)
            
            ' if found, colour it
            If Not rngNext Is Nothing Then
                Worksheets(i).Activate
                rngNext.Interior.Color = CI
                rngNext.Select
                If rngNext.Address <= oldAddress Then
                    flag = False
                End If
                oldAddress = rngNext.Address
            Else
                flag = False
            End If
            Loop
        Next i
    End If
    Sheets("Consolidated").Activate
Next cl

With Application
    .ScreenUpdating = CurrentScreenUpdating
    .Calculation = CurrentCalculate
    .EnableEvents = CurrentEnableEvents
End With
End Sub
 
Last edited:
Upvote 0
Thanx for the help.... it works... well while i ran the code on a particular sheet it highlighted only three lines and it did not highlight the fourth line... so i tried copying a few more lines below it and but it still did not highlight more than three lines for that sheet, so i copied and pasted that line on another sheet and it worked fine... i dont know if i ran the code wrongly... Kindly recheck the code... I feel something must have gone wrong at my end...

Thank You
 
Upvote 0
I rechecked the code... looks good to me. However, I made a small change. Test this:

Code:
Sub replaceColours()
Dim cl As Range
Dim CI As Long
Dim i As Integer
Dim strText As String
Dim rngNext As Range

With Application
    CurrentScreenUpdating = .ScreenUpdating
    CurrentCalculate = .Calculation
    CurrentEnableEvents = .EnableEvents
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

Sheets("Consolidated").Activate

For Each cl In ActiveSheet.UsedRange.Columns(1).Cells
    CI = cl.Interior.Color
    cl.Select
    If CI <> 16777215 Then
        strText = cl.Value
        For i = 1 To Worksheets.Count
            oldAddress = 0
            flag = True
            Do Until flag = False
            ' find next instance of strText
            Set rngNext = Worksheets(i).Cells.Find(What:=strText, After:=ActiveCell, LookIn:= _
                xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
                xlNext, MatchCase:=False, SearchFormat:=False)
            
            ' if found, colour it
            If Not rngNext Is Nothing Then
                Worksheets(i).Activate
                rngNext.Interior.Color = CI
                rngNext.Select
                If rngNext.Row <= oldAddress Then
                    flag = False
                End If
                oldAddress = rngNext.Row
            Else
                flag = False
            End If
            Loop
        Next i
    End If
    Sheets("Consolidated").Activate
Next cl

With Application
    .ScreenUpdating = CurrentScreenUpdating
    .Calculation = CurrentCalculate
    .EnableEvents = CurrentEnableEvents
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,589
Messages
6,179,744
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top