如果ID +条件是唯一的,如何签入VBA

时间:2018-04-16 12:36:02

标签: arrays vba

我完全是VBA的新手(以及StackOverflow!),我很快就学到了所需的技能,但我遇到了一个问题。

我的Excel文件目前看起来像这样:

|    ID    | ... |  Condition  | ...
|id1       | ... |default      | ...
|id1       | ... |One; Two     | ...
|id2       | ... |default; Two | ...
|id3       | ... |One, default | ...

我需要检查几件事。到目前为止,没问题。除了一件事:如何检查特定ID是否具有特定条件(假设id1具有条件“Two”),则不能再有具有相同ID和相同条件的另一行。

具体方面:

  • 除了强制性的“默认”条件外,程序不应该知道不同的条件是什么,也不知道会有多少条件。
  • 我无法更改公司中使用的Excel文件。
  • 每个ID至少具有“默认”条件(我知道如何检查),但除了默认值之外,可以从0到可能有很多条件(永远不会超过10,但是...... )。

我知道如何使用已知条件,为每个条件创建一个数组,遍历每一行,在所述条件的数组中添加ID,除非它已经存在。不幸的是,我不符合这些要求。

更新

澄清:我称之为唯一组合的是一个id和一个条件。在我给出的Excel示例中,我们只有唯一的组合:id1 + default,id1 + One,id1 + Two,id2 + default,id2 + Two等等......以下示例包含我需要查找的问题:< / p>

|    ID    | ... |  Condition  | ...
|id1       | ... |default      | ...
|id1       | ... |One; Two     | ...
|id2       | ... |default; Two | ...
|id2       | ... |One, default | ...

这里有两倍的组合“id2 + default”。这是我的VBA程序必须找到的错误。

1 个答案:

答案 0 :(得分:0)

检查唯一性的一种简单方法是使用字典。 (在项目中添加对Microsoft Scripting Runtime的引用以获取它的IntelliSense。如果您不想要该引用,请使用注释掉的声明。)

Dim D As Scripting.Dictionary
' Dim D as Object
Set D = New Scripting.Dictionary
' Set D = CreateObject("Scripting.Dictionary")

Dim K As String
K = GenerateKeyString()
If D.Exists(K) Then
    ' Uniqueness violated
    ' your code here...
Else
    ' Add key with empty content, as the value is not important to only check for uniqueness
    D.Add K, vbNullString
End If

GenerateKeyString()函数会返回ID1_ConditionOne之类的内容 - 这个字符串看起来到底是怎么回事。它的创造是一致的,这一点非常重要。

让我尝试一下你可以改进的粗略草图:

Option Explicit

Public Sub FindNonUniqueRows()
    Dim Ws As Worksheet
    Set Ws = ActiveWorkbook.Sheets("YourSheetNameHere")
    Dim D As Scripting.Dictionary
    Set D = New Scripting.Dictionary
    Dim StartRow As Long, CondCol As Long, IdCol As Long
    StartRow = 1    ' Starting row of your data
    IdCol = 1       ' Index column
    CondCol = 3     ' Condition column

    Dim r As Long
    r = StartRow
    ' Loop over each row until encountering an empty cell in the ID column
    Do While Ws.Cells(r, IdCol) <> vbNullString

        Dim Id As String
        Id = Ws.Cells(r, IdCol)
        ' Loop over each condition to add the ID/Condition pair to the dictionary
        Dim cond As Variant
        For Each cond In Split(Ws.Cells(r, CondCol), ";")

            ' Trim the string to avoid complications with leading or trailing blanks
            Dim condTrim As String
            condTrim = Trim(cond)

            Dim UniqueKey As String
            UniqueKey = GenerateKeyString(Id, condTrim)

            If D.Exists(UniqueKey) Then

                ' Uniqueness violated
                ' your code here...
                MsgBox "Combination of ID '" & Id & "' and condition '" & condTrim & "' already exists!"

            Else

                ' Add key with empty content, as the value is not important to only check for uniqueness
                D.Add UniqueKey, vbNullString

            End If

        Next cond
        r = r + 1
    Loop
End Sub

Private Function GenerateKeyString(ByVal Id As String, ByVal Condition As String) As String
    GenerateKeyString = Id & "_" & Condition
End Function

请注意,您可以直接通过Split()生成的StringArray迭代遍历元素,而无需先将其分配给变量。