使用macrosvalidationexcel中的唯一组合键

我正在创build一个excelmacros。 作为一部分,我需要在Excel中validation唯一的组合键。 即说Column1 + Column2 + Column3不应该重复。

如果是的话,这行应该用红色突出显示。

什么是最好的办法呢?

提前致谢

有几种方法可以实现这一点:两个取决于对数据进行sorting,而另一个则不取决于数据sorting。 我会给他们提供不同的答案,以便读者可以指出他们更喜欢哪一个。

使用VBA中的字典识别重复项

临:快速,不需要分类
Con:需要代码,不会自动更新

在这种情况下,我会将突出显示重复行的实际问题分开处理。 该函数返回一个Dictionary,其中的键是具有多于一行的复合键,值是包含与该键匹配的所有行的行号的集合。 它相当于.NET中的Dictionary<string,List<int>> 。 从概念上来说,它看起来像这样

 "some..key..1" : [1, 42, 401] "some..key..2" : [134, 135] 

键是每个键列的连接内容,由空字符分隔。 我使用不可打印的空字符,使得键集(“A”,“Dog”,“2”)不等于(“AD”,“o”,“g2”)。

正如所写的,关键的比较是区分大小写的。 如果需要不区分大小写的匹配,请将dctValuesdctDuplicatesCompareMode属性设置为TextCompare

注意 :您需要添加对Microsoft脚本运行时的引用

 Public Function FindDuplicates(ByVal DataRange As Range, ParamArray KeyColumns()) As Dictionary Dim ws As Worksheet Dim vKeyRange, rngCol As Range Dim dctKeys As New Dictionary Dim colKeys Dim keyParts() As String Dim strKey As String Dim dctValues As New Dictionary Dim dctDuplicates As New Dictionary Dim i As Long, ub As Long Dim lngFirstRow As Long, lngLastRow As Long, lngRow As Long Set ws = DataRange.Worksheet ' Identify unique key column numbers For Each vKeyRange In KeyColumns For Each rngCol In vKeyRange.Columns dctKeys(rngCol.Column) = True Next Next colKeys = dctKeys.Keys ub = UBound(colKeys) ReDim keyParts(ub) ' Find first and last row of data range lngFirstRow = DataRange.Cells(1, 1).Row lngLastRow = DataRange.Cells(DataRange.Rows.Count, 1).Row ' Loop through rows For lngRow = lngFirstRow To lngLastRow ' Get the parts for the key For i = 0 To ub keyParts(i) = ws.Cells(lngRow, colKeys(i)).Value Next ' Concatenate the parts with an unprintable character as ' the delimiter, so that "A" + "Dog" != "AD" + "og" strKey = Join(keyParts, Chr(0)) ' If the key hasn't been found yet, create a new collection If Not dctValues.Exists(strKey) Then dctValues.Add strKey, New Collection End If ' Push the row number to the list of rows with this key dctValues(strKey).Add lngRow ' If this is the second row with this key, add the ' list to the dictionary of keys with multiple rows If dctValues(strKey).Count = 2 Then dctDuplicates.Add strKey, dctValues(strKey) End If Next Set FindDuplicates = dctDuplicates End Function 

用法:在A2:I5000中查找所有重复的行,使用列A,B和E作为关键列

 Dim ws As Worksheet, dctDups As Dictionary, vKey, vRow Set ws = ThisWorkbook.Worksheets(1) Set dctDups = FindDuplicates(ws.Range("A2:I5000"), ws.Range("A:B"), ws.Range("E:E")) For Each vKey In dctDups For Each vRow In dctDups(vKey) ws.Range("A" & vRow & ":I" & vRow).Interior.Color = vbRed Next Next 

有几种方法可以实现这一点:两个取决于对数据进行sorting,而另一个则不取决于数据sorting。 我会给他们提供不同的答案,以便读者可以指出他们更喜欢哪一个。

sorting和应用条件格式

临:dynamic(调整到数据的变化),不需要任何代码
骗局:要求sorting,可以变得凌乱

  1. 按键列手动sorting
  2. 创build条件格式规则并将其应用于所有数据行。

    1. 突出显示所有数据,但是从第一行数据开始
    2. select条件格式 – >新规则
    3. 将格式设置为红色填充
    4. select“使用公式来确定要格式化的单元格”
    5. 假设您的select从第2行开始 (第1行有一个标题),并且您的键列是A,B和C,则需要以下公式。 请仔细注意$符号出现的位置和不符合的地方:

       =OR((CONCATENATE($A2,$B2,$C2)=CONCATENATE($A1,$B1,$C1)), (CONCATENATE($A2,$B2,$C2)=CONCATENATE($A3,$B3,$C3))) 

      这将突出显示具有重复键的行,或者如果有多于两个的行,则突出显示所有行。