我试图在一个包含12个工作表的工作簿中突出显示重复项。
我们跟踪ID号码,如果任何另一个工作表中有相同的ID号码(数值),我想突出显示该单元格。
当我在"This Workbook"中使用以下代码时,它只应用于一个工作表而不是多个工作表。
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim Rng As Range
Dim cel As Range
Dim col As Range
Dim c As Range
Dim firstAddress As String
'Duplicates will be highlighted in red
Target.Interior.ColorIndex = xlNone
For Each col In Target.Columns
Set Rng = Range(Cells(1, col.Column), Cells(Rows.Count, col.Column).End(xlUp))
Debug.Print Rng.Address
For Each cel In col
If WorksheetFunction.CountIf(Rng, cel.Value) > 1 Then
Set c = Rng.Find(What:=cel.Value, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.ColorIndex = 3
Set c = Rng.FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End If
Next
Next col
For Each Worksheet In ActiveWorkbook.Worksheets
循环遍历每个工作表,并将代码应用于每个工作表。 - dosdel