为什么多个连续的不等条件在vba中不起作用?

Max*_*III 5 excel vba if-statement excel-vba

我想知道为什么以下语法不能像我在VBA中那样工作,以及我应该做些什么来确保它呢.

For a = 1 To 10
    For b = 1 To 10
        For c = 1 To 10
            If a <> b <> c Then
                MsgBox (a & " " & b & " " & c)
            End If
        Next c
    Next b
Next a
Run Code Online (Sandbox Code Playgroud)

这是一个简化的示例,仍然可以通过以下方式手动获取:

if a<>b and b<>c and c<>a then
Run Code Online (Sandbox Code Playgroud)

但是我实际想要的代码多次有10个这样的变量,这使得它在55个不等条件下不可行,或者我可能会输错.我认为有一种更有效的方法,但我还没有找到它.

PS.我的目标是,如果所有变量都是唯一的,则只会弹出一个消息框.

我已经实现了我的目标,尽管它可能比以下方法更有效率:

For a = 1 To 10
    check(a) = True
    For b = 1 To 10
        If check(b) = False Then
        check(b) = True
        For c = 1 To 10
            If check(c) = False Then
                check(c) = True
                For d = 1 To 10
                    If check(d) = False Then
                        check(d) = True
                        For e = 1 To 10
                            If check(e) = False Then
                                check(e) = True
                                MsgBox (a & " " & b & " " & c & " " & d & " " & e)
                            End If
                            check(e) = False
                            check(a) = True
                            check(b) = True
                            check(c) = True
                            check(d) = True
                        Next e
                    End If
                    check(d) = False
                    check(a) = True
                    check(b) = True
                    check(c) = True
                Next d
            End If
            check(c) = False
            check(a) = True
            check(b) = True


        Next c
        End If
        check(b) = False
        check(a) = True

    Next b
Next a
Run Code Online (Sandbox Code Playgroud)

Joh*_*man 3

这是用于枚举排列的Johnson-Trotter 算法的实现。这是我在研究旅行商问题的强力解决方案时编写的一个小修改。请注意,它返回一个二维数组,这可能会消耗大量内存。可以对其进行重构,使其成为消耗而不是存储排列的子组件。只需将靠近底部的代码部分(当前排列perm存储在数组 中perms)替换为使用该排列的代码即可。

Function Permutations(n As Long) As Variant
'implements Johnson-Trotter algorithm for
'listing permutations. Returns results as a variant array
'Thus not feasible for n > 10 or so

    Dim perm As Variant, perms As Variant
    Dim i As Long, j As Long, k As Long, r As Long, D As Long, m As Long
    Dim p_i As Long, p_j As Long
    Dim state As Variant

    m = Application.WorksheetFunction.Fact(n)
    ReDim perm(1 To n)
    ReDim perms(1 To m, 1 To n) As Integer
    ReDim state(1 To n, 1 To 2) 'state(i,1) = where item i is currently in perm
                                'state(i,2) = direction of i

    k = 1 'will point to current permutation
    For i = 1 To n
        perm(i) = i
        perms(k, i) = i
        state(i, 1) = i
        state(i, 2) = -1
    Next i
    state(1, 2) = 0
    i = n 'from here on out, i will denote the largest moving
          'will be 0 at the end
    Do While i > 0
        D = state(i, 2)
        'swap
        p_i = state(i, 1)
        p_j = p_i + D
        j = perm(p_j)
        perm(p_i) = j
        state(i, 1) = p_j
        perm(p_j) = i
        state(j, 1) = p_i
        p_i = p_j
        If p_i = 1 Or p_i = n Then
            state(i, 2) = 0
        Else
            p_j = p_i + D
            If perm(p_j) > i Then state(i, 2) = 0
        End If
        For j = i + 1 To n
            If state(j, 1) < p_i Then
                state(j, 2) = 1
            Else
                state(j, 2) = -1
            End If
        Next j
        'now find i for next pass through loop
        If i < n Then
            i = n
        Else
            i = 0
            For j = 1 To n
                If state(j, 2) <> 0 And j > i Then i = j
            Next j
        End If
        'record perm in perms:
        k = k + 1
        For r = 1 To n
            perms(k, r) = perm(r)
        Next r
    Loop
    Permutations = perms
End Function
Run Code Online (Sandbox Code Playgroud)

测试如下:

Sub test()
    Range("A1:G5040").Value = Permutations(7)
    Dim A As Variant, i As Long, s As String
    A = Permutations(10)
    For i = 1 To 10
        s = s & " " & A(3628800, i)
    Next i
    Debug.Print s
End Sub
Run Code Online (Sandbox Code Playgroud)

前 20 行输出如下所示:

在此输入图像描述

此外,2 1 3 4 5 6 7 8 9 10还会在立即窗口中打印。我的第一个版本使用了普通变体,并导致n = 10. 我对其进行了调整,使其perms重新尺寸化以包含整数(与变体相比,它消耗的内存更少),并且现在能够处理10. 在我的机器上运行测试代码大约需要 10 秒。