Excel VBA Duplicate operation Not Repeating over multiple sheets

weepingpea

New Member
Joined
Sep 7, 2015
Messages
9
Hi everyone,

I adapted this macro that allows me to highlight duplicates in column A of every sheet in my workbook. There are 5 sheets. When I press F8 and have the sheet open, the macro successfully highlights the duplicates. However, when I run F5, it does not work, or sometimes only works for one of the sheets.

I work in PC Excel 2010. Could you see what is wrong with this code?

Many thanks for your help!

weepingpea

Code:
Sub HLDupeswithinSheets()
    Dim myColumn As Long
    Dim i As Integer
    Dim columnCount As Long
    Dim lastRow As Long
    Dim dupeColor As Long
    Dim w1 As Worksheet, w2 As Worksheet, w3 As Worksheet, w4 As Worksheet, w5 As Worksheet
    
    Set w1 = ThisWorkbook.Sheets("1_InProgress")
    Set w2 = ThisWorkbook.Sheets("2_Prospects")
    Set w3 = ThisWorkbook.Sheets("3_CVReview")
    Set w4 = ThisWorkbook.Sheets("4_Events")
    Set w5 = ThisWorkbook.Sheets("5_Others")
    
    columnCount = 1
    dupeColor = 13551615


With w1
    For i = 1 To columnCount
        lastRow = w1.Cells(w1.Rows.Count, i).End(xlUp).Row
        Call HighlightDupesInRange(dupeColor, Cells(1, i).Resize(lastRow, 1))
        dupeColor = dupeColor + 15
    Next i
End With
With w2
For i = 1 To columnCount
        lastRow = w2.Cells(w2.Rows.Count, i).End(xlUp).Row
        Call HighlightDupesInRange(dupeColor, Cells(1, i).Resize(lastRow, 1))
        dupeColor = dupeColor + 15
Next i
End With
With w3
For i = 1 To columnCount
        lastRow = w3.Cells(w3.Rows.Count, i).End(xlUp).Row
        Call HighlightDupesInRange(dupeColor, Cells(1, i).Resize(lastRow, 1))
        dupeColor = dupeColor + 15
Next i
End With
With w4
For i = 1 To columnCount
        lastRow = w4.Cells(w4.Rows.Count, i).End(xlUp).Row
        Call HighlightDupesInRange(dupeColor, Cells(1, i).Resize(lastRow, 1))
        dupeColor = dupeColor + 15
Next i
End With
With w5
For i = 1 To columnCount
        lastRow = w5.Cells(w5.Rows.Count, i).End(xlUp).Row
        Call HighlightDupesInRange(dupeColor, Cells(1, i).Resize(lastRow, 1))
        dupeColor = dupeColor + 15
Next i
End With
End Sub


Sub HighlightDupesInRange(cellColor As Long, rng As Range)
dupeColor = 13551615
    With rng
        '.FormatConditions.Delete
        .FormatConditions.AddUniqueValues
        .FormatConditions(1).DupeUnique = xlDuplicate
        .FormatConditions(1).Interior.Color = dupeColor
        .FormatConditions(1).StopIfTrue = False
    End With
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Make sure you pqass the worksheet to your HighlightDupes routine.

Rich (BB code):
Call HighlightDupesInRange(dupeColor, w1.Cells(1, i).Resize(lastRow, 1))

Otherwise Excel will guess and just use the activesheet.
 
Upvote 0

Forum statistics

Threads
1,215,424
Messages
6,124,817
Members
449,190
Latest member
rscraig11

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