【发布时间】:2014-10-04 18:52:36
【问题描述】:
我正在尝试突出显示 12 张工作簿中的重复项。
我们跟踪 ID#,如果 ID#(值)在任何其他工作表上,我想突出显示单元格。
当我在“此工作簿”中使用下面的代码时,它适用于一个工作表,而不是跨多个工作表。
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遍历每张工作表并将代码应用于每张工作表。 -
所有工作表中的所有 ID 都在同一列中吗?
-
是的,ID#s 都在每个工作表的“A”列中。
-
发布了答案。看看吧
-
您可能想刷新页面,因为我正在尝试代码并进行了一些编辑。