vba - Excel 如何查找连续的单元格或值?

标签 vba excel duplicates

我有一个数据集,其中每一行都属于一个唯一的人,所以我想要做的是在每一行中找到重复的值。

我尝试使用条件格式,但它非常耗时,因为我必须将其应用到每一行,否则它将在所有行中找到重复项,而不仅仅是一行。

enter image description here

您能否建议一些可以帮助我的东西,可以是公式、VBA 或条件格式公式。

我使用宏记录器来创建宏,结果如下。如果我可以让它遍历一系列行并应用可能有帮助的格式

Sub DuplicatesRow1() ' ' DuplicatesRow Macro '

'
    Rows("251:251").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Rows("252:252").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Rows("253:253").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("E259").Select End Sub

最佳答案

我进一步研究了这个问题,并设法想出了以下似乎对我有用的代码。我是 VBA 新手,没有足够的经验,所以请告诉我我的代码是否可以进一步改进

Private Sub HighlightDuplicateRow(row As Integer)

Dim report As Worksheet
Set report = Excel.ActiveSheet
report.Cells(row, row).EntireRow.Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub


Sub DuplicatesInEachRow()
Dim counter As Integer, limit As Variant
counter = 2
limit = InputBox("Give me last row number", "Highlight Duplicates in a Row")
If limit = "" Then Exit Sub
Do Until counter > limit
Call HighlightDuplicateRow(counter)
counter = counter + 1
Loop
End Sub

关于vba - Excel 如何查找连续的单元格或值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926006/

相关文章:

vba - NumberFormat 有 0 位小数,但有千位分隔符 "' "

JavaScript/VBA 自动化 : Changing the onclick attribute

ms-access - Access 07 VBA 通过路径获取表

xml - VBA XML 解析——遍历子节点

vba - Excel 完全限定范围 (Cells(),Cells())

mySQL + vba,检查表中是否已存在某个项目

c# - 通过 OleDb 使用 Microsoft Access 数据库引擎将 DataTable 插入 Excel

mysql - 我如何计算 ON DUPLICATE KEY UPDATE MySQL -php 中更新行的总数

matlab - 如何找到在 matlab 中重复的矩阵的每一行的索引?

php - 检查重复行并更新状态,保持第一个数据不变