Excel VBA: 筛选相近或相同单元格内容
Excel VBA: 筛选相近或相同单元格内容
这是一个简单的VBA代码,可以在Excel中筛选相近或相同的单元格:
Sub FindSimilarCells()
Dim rng As Range
Dim cell As Range
Dim i As Integer, j As Integer
Dim match As Boolean
Set rng = ActiveSheet.UsedRange
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
If Not IsEmpty(rng.Cells(i, j)) Then
Set cell = rng.Find(What:=rng.Cells(i, j), After:=rng.Cells(i, j), LookIn:=xlValues, LookAt:=xlWhole)
If Not cell Is Nothing Then
match = True
If cell.Row <> i Or cell.Column <> j Then
match = False
End If
If match Then
cell.Interior.ColorIndex = 6 'highlight the matching cell
End If
End If
End If
Next j
Next i
End Sub
该代码循环遍历所有单元格,并在每个单元格中搜索与其相同的单元格。如果找到相同的单元格,则将其突出显示。
请注意,该代码使用'xlWhole'参数在单元格中查找完全匹配的值。如果您想查找部分匹配的值,可以将参数更改为'xlPart'。
原文地址: https://www.cveoy.top/t/topic/neRv 著作权归作者所有。请勿转载和采集!