VBA匹配没有重复的对

时间:2017-09-13 18:24:39

标签: vba excel-vba excel

我被要求做的是将用户输入作为整数。例如,6是输入。 6被传递到等式(6-1)*(6)/ 2中,这相当于15对。然后我调用另一个将输入处理成一组{for next}循环的子。我有一个If语句检查重复,但它是有限的。我已经尝试将迭代变量传递给if语句,但这将无法正常工作。

以下是代码:

For intOuter = 1 To options - 1

    'The intInner loop cycles through and sets up the loop for the combination and determines if
    'the combination has been made already

        For intInner = 1 To options

    'If statement tests if a combination has already been made if it has it will not be printed
    'if it has not been combined then it will be printed to the immediate window


                If intOuter <> intInner And intOuter <> value + 1 And intOuter <> value + 2 And intOuter <> value + 3 _
                And intOuter <> value + 4 Then
                       Debug.Print intOuter & " vs " & intInner & "  Actual"
                End If

        Next intInner

    Next intOuter

此示例的输出正确显示(用户输入为6)

1vs2
1vs3
1vs4
1vs5
1vs6
2vs3
2vs4
2vs5
2vs6
3vs4
3vs5
3vs6
4vs5
4vs6
5vs6

先谢谢,我真的感谢它。

1 个答案:

答案 0 :(得分:1)

看起来你很亲密!试试这个:

   Dim intInner As Integer
   Dim intOuter As Integer

   For intOuter = 1 To Options - 1
       For intInner = intOuter + 1 To Options
          Debug.Print intOuter & "vs" & intInner
       Next
   Next