Excel 在VBA中突出显示重复项之间的差异
您好,我有一个电子表格,其中包含以下列:Excel 在VBA中突出显示重复项之间的差异,excel,vba,Excel,Vba,您好,我有一个电子表格,其中包含以下列: Transaction_ID counter State File_Date Date_of_Service Claim_Status NDC_9 Drug_Name Manufacturer Quantity Original_Patient_Pay_Amount Patient_Out_of_Pocket eVoucher_Amount WAC_per_Unit__most_recent_ RelayHeal
Transaction_ID counter State File_Date Date_of_Service Claim_Status NDC_9 Drug_Name Manufacturer Quantity Original_Patient_Pay_Amount Patient_Out_of_Pocket eVoucher_Amount WAC_per_Unit__most_recent_ RelayHealth_Admin_Fee Total_Voucher_Charge Raw_File_Name
这里有重复的事务ID。是否有VBA会突出显示两行之间的差异?因此,可能存在具有相同事务ID的数据,但我想强调的是,它们可能具有不同的其他字段,因此它们不是真正重复的,并且希望看到哪些信息是不同的
谢谢 假设:
Option Explicit
Public Sub HighlightUniqueValues()
Dim r As Long, c As Long 'row and column counters
Dim LastCol As Long, LastRow As Long 'right-most and bottom-most column and row
Dim ColLetter As String
Dim RepeatValues As Long
'get right-most used column
LastCol = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column
'get bottom-most used row
LastRow = Me.Cells(Me.Rows.Count, "A").End(xlUp).Row
'assume first column has the main ID
For r = 2 To LastRow 'skip the top row, which presumably holds the column headers
For c = 2 To LastCol 'skip the left-most column, which should contain the ID
'Get column letter
ColLetter = Split(Cells(1, c).Address(True, False), "$")(0)
' Count the number of repeat values in the current
'column associated with the same value in the
'left-most column
RepeatValues = WorksheetFunction.CountIfs(Range("A:A"), Range("A" & r), Range(ColLetter & ":" & ColLetter), Range(ColLetter & r))
' If there is only one instance, then it's a lone
'value (unique for that ID) and should be highlighted
If RepeatValues = 1 Then
Range(ColLetter & r).Interior.ColorIndex = 6 'yellow background
Else
Range(ColLetter & r).Interior.ColorIndex = 0 'white background
End If
Next c
Next r
End Sub
e、 g.Excel的“查找重复项”条件格式应足以满足此要求。问题是,它只在一列中有效
因此,可能存在具有相同事务ID的数据,但我想强调的是,它们可能具有不同的其他字段,因此它们不是真正的重复项
因此,您可以尝试添加一个新列,并在该新列中连接组合值应唯一的所有列,然后在该列上运行Excel的“查找重复项”条件格式,而不是单独跟踪事务ID列中的重复项
例如,如果[Transaction_ID]、[File_Date]和[NDC_9]的组合应该是唯一的,则创建一个组合了[Transaction_ID]、[File_Date]和[NDC_9]列值的新列-假设您的数据在实际表中,您可以使用如下表公式:
=[@Transaction_ID]&[@File_Date]&[@NDC_9]
并想看看哪些信息是不同的
然后可以过滤该列中的重复项,然后查看其他列,可以看到它们的不同之处。您的问题措辞不可能比这更具体……添加一列以连接构成“唯一键”的所有列,然后使用条件格式->突出显示该列上的重复项。可能是通过ADO的SQL,按ID分组。它也可以通过数组公式来完成。它必须是VBA,还是新列中的一个简单函数就足够了?在你如何提出这个问题时,有很多模棱两可的地方。e、 g.数据集是否以任何特定方式排序?这将大大简化或使答案复杂化。Mat的Mug-concat+条件格式工作得非常好,非常感谢,这正是我所需要的!