如何检查VBA中预定义集列表中是否出现一组字符串?

时间:2016-09-22 18:35:52

标签: excel vba excel-vba

我对VBA很陌生,这很难解释,但希望下面的例子能说明我想要做的事情。

我之前使用过以下功能:

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

返回指定字符串是否包含在值列表中,如下所示:

Dim CL3(3) as String
CL3(0) = item1
CL3(1) = item2
CL3(2) = item5

If IsInArray(someValue, CL3) Then SomeAction

但是,现在我试图检查指定的字符串集是否出现在特定的集合列表中。

我有字符abc。从工作表中检索这些值:

Dim a, b, c As String
a = Range(SomeCell1).Value
b = Range(SomeCell2).Value
c = Range(SomeCell3).Value

我喜欢类似于当我试图查看单个字符串时的类似内容,如下所示:

Dim CL3(8) as Variant
CL3(0) = item1, item2, item3
CL3(1) = item2, item1, item4
CL3(2) = item5, item3, item6
...
CL3(7) = item3, item4, item1

有许多不同的item,但是这些只有8种可能的有效组合。 item可以按任意顺序分配给abc,因此列表中定义的顺序不一定与{{1}的顺序相对应}},ab

如果可能,我还想知道cab中的哪一个是第一,第二和第三c根据在定义的列表中设置。

我也希望能够使用包含4个项目的集合和包含5个项目的集合来执行此操作。

总而言之,我想:

  • 定义集合列表
  • 检查指定的itemab是否为指定的一组
  • 如果是,请c涉及指定集合中的第一个SomeAction
  • 循环到下一个itemab(没有设定的数字可循环播放)

谢谢。

3 个答案:

答案 0 :(得分:0)

这是一个如何做类似事情的例子。它不是完成的产品,但它应该给你一些好主意。请注意 A1:A6 包含我的"项目"例如:

A        B        C
Item1    Item2    Item3
Item2    Item1    Item3
...

然后代码如下:

Sub test()
    Dim rng As Range
    Dim i As Integer

    For Each rng In Sheet1.Range("A1:C6").Rows
        For i = 1 To rng.Columns.Count
            Select Case UCase(rng.Cells(1, i).Value)
                Case "A", "B", "C"
                    DoSomething rng.Cells(1, 1).Value
            End Select
        Next i
    Next rng
End Sub

Sub DoSomething(str As String)
    'Debug.Print str
End Sub

答案 1 :(得分:0)

针对主要问题尝试此(几乎)伪代码(未经测试):

Dim a As String, b As String, c As String
a = Range(SomeCell1).Value
b = Range(SomeCell2).Value
c = Range(SomeCell3).Value

Dim CL3(8) as Variant
CL3(0) = Array(item1, item2, item3)
CL3(1) = Array(item2, item1, item4)
CL3(2) = Array(item5, item3, item6)
...
CL3(7) = Array(item3, item4, item1)

Dim i As Long
Dim foundabc As Boolean
For i = 0 to 7
    If IsInArray(a, CL3(i)) Then
        If IsInArray(b, CL3(i)) Then
            If IsInArray(c, CL3(i)) Then
                foundabc = True
                Exit For
            End If
        End If
    End If
Next i

If foundabc Then SomeAction

答案 2 :(得分:0)

这样的事情:

Sub Tester()

    Dim CL3(3) As Variant, T, arrpos()
    Dim i As Long, n As Long,  m

    CL3(0) = Array("item1", "item2", "item3")
    CL3(1) = Array("item4", "item5", "item6")
    CL3(2) = Array("item7", "item9", "item8")
    CL3(3) = Array("item5", "item6", "item7")
    '....rest of your series here

    T = Array("item7", "item8", "item9")
    ReDim arrpos(LBound(T) To UBound(T))

    For i = LBound(CL3) To UBound(CL3)
        For n = LBound(T) To UBound(T)
            m = Application.Match(T(n), CL3(i), 0)
            If IsError(m) Then Exit For

            'got match:store position
            arrpos(n) = m
            If n = UBound(T) Then
                ' we're on the last check, so done...
                Debug.Print "Match at positions " & _
                     Join(arrpos, ",") & " in cl3(" & i & ")"
                Exit Sub
            End If

        Next n
    Next i
End Sub