查找重复项,如果单元格旁边是这个并且这个值执行此操作
Find Duplicates, if beside cells is this and this value do this
列A-------------列B
重复号码----M-946
重复号码----M-954
反之亦然
重复号码----M-954
重复号码----M-946
如果在每组 2 个重复项中,ColumnB 中的值如上例所述,则为真。然后为 A 列和 B 列涂上不同的颜色。
为仅重复 2 次的行着色的代码。
看到这个:
Sub find2duplicatesonly()
Dim cel As Variant
Dim myrng As Range
Set myrng = Range(Range("A2"), Range("A2").End(xlDown))
myrng.Interior.ColorIndex = xlNone
For Each cel In myrng
clr = 10
If Application.WorksheetFunction.CountIf(myrng, cel) = 2 Then
cel.Interior.ColorIndex = 26
clr = clr + 10
End If
Next
MsgBox ("All duplicates found and coloured")
End Sub
这是我在尝试:
我哪里错了?
Public Sub testcode1()
Dim rngFound As Range
Dim strFirst As String
Dim varFind As Variant
For Each varFind In Array("M-954", "M-946")
Set rngFound = Columns("B").Find(varFind, Cells(Rows.Count, "B"), xlValues, xlPart)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Do
Set rngFound = Columns("B").Find(varFind, rngFound, xlValues, xlPart)
Loop While rngFound.Address <> strFirst
Select Case varFind
Case "M-954" & "M-946": Call find2duplicatesonly
End Select
End If
Next varFind
End Sub
您可以使用 Conditional Formatting. That page mentions how to do specifically duplicates, but if your "duplicates search" is a little more complex, you can look into making your own formula for the conditional formatting here
编辑:经过讨论(见下面的评论),条件格式可能有效,但我无法突出显示两个单元格,一次只能突出显示一个单元格。因此我写了这个 VB 代码:
Sub Macro1()
Dim lastRow As Double, matchString1 As String, matchString2 As String
Dim ws As Worksheet
Dim rng As Range, cel As Range
Set ws = ActiveSheet
matchString1 = "Superman"
matchString2 = "Batman"
With ws
lastRow = .Cells(1, 1).End(xlDown).Row
Set rng = .Range(.Cells(1, 1), .Cells(lastRow, 1))
For Each cel In rng
If (cel.Value = matchString1 And cel.Offset(1, 0).Value = matchString2) Or _
(cel.Value = matchString2 And cel.Offset(1, 0).Value = matchString1) Then
With .Range(.Cells(cel.Row, cel.Column), .Cells(cel.Row + 1, cel.Column)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
End With
End If
Next cel
End With
End Sub
请注意,您可以根据需要更改 "Superman" 和 "Batman"。此外,如果每次都更改,您可以将其设置为引用单元格的值(即 matchString1 = Cells(1,2).Value
)。这导致了这种着色:Screenshot
此外,您需要对前三个 "Batman" 做些什么吗?
列A-------------列B
重复号码----M-946
重复号码----M-954
反之亦然
重复号码----M-954
重复号码----M-946
如果在每组 2 个重复项中,ColumnB 中的值如上例所述,则为真。然后为 A 列和 B 列涂上不同的颜色。
为仅重复 2 次的行着色的代码。 看到这个:
Sub find2duplicatesonly()
Dim cel As Variant
Dim myrng As Range
Set myrng = Range(Range("A2"), Range("A2").End(xlDown))
myrng.Interior.ColorIndex = xlNone
For Each cel In myrng
clr = 10
If Application.WorksheetFunction.CountIf(myrng, cel) = 2 Then
cel.Interior.ColorIndex = 26
clr = clr + 10
End If
Next
MsgBox ("All duplicates found and coloured")
End Sub
这是我在尝试:
我哪里错了?
Public Sub testcode1()
Dim rngFound As Range
Dim strFirst As String
Dim varFind As Variant
For Each varFind In Array("M-954", "M-946")
Set rngFound = Columns("B").Find(varFind, Cells(Rows.Count, "B"), xlValues, xlPart)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Do
Set rngFound = Columns("B").Find(varFind, rngFound, xlValues, xlPart)
Loop While rngFound.Address <> strFirst
Select Case varFind
Case "M-954" & "M-946": Call find2duplicatesonly
End Select
End If
Next varFind
End Sub
您可以使用 Conditional Formatting. That page mentions how to do specifically duplicates, but if your "duplicates search" is a little more complex, you can look into making your own formula for the conditional formatting here
编辑:经过讨论(见下面的评论),条件格式可能有效,但我无法突出显示两个单元格,一次只能突出显示一个单元格。因此我写了这个 VB 代码:
Sub Macro1()
Dim lastRow As Double, matchString1 As String, matchString2 As String
Dim ws As Worksheet
Dim rng As Range, cel As Range
Set ws = ActiveSheet
matchString1 = "Superman"
matchString2 = "Batman"
With ws
lastRow = .Cells(1, 1).End(xlDown).Row
Set rng = .Range(.Cells(1, 1), .Cells(lastRow, 1))
For Each cel In rng
If (cel.Value = matchString1 And cel.Offset(1, 0).Value = matchString2) Or _
(cel.Value = matchString2 And cel.Offset(1, 0).Value = matchString1) Then
With .Range(.Cells(cel.Row, cel.Column), .Cells(cel.Row + 1, cel.Column)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
End With
End If
Next cel
End With
End Sub
请注意,您可以根据需要更改 "Superman" 和 "Batman"。此外,如果每次都更改,您可以将其设置为引用单元格的值(即 matchString1 = Cells(1,2).Value
)。这导致了这种着色:Screenshot
此外,您需要对前三个 "Batman" 做些什么吗?