2017-09-13 44 views
-1

我被要求做的是把一個用戶輸入作爲一個整數。例如,6是輸入。 6變成等式(6-1)*(6)/ 2,這相當於15對。然後,我再調用另一個將輸入處理爲{for next}循環的子集。我有一個If語句來檢查重複項,但它是有限的。我曾嘗試將迭代變量傳入if語句中,但這樣做無法正常工作。VBA匹配對沒有重複

下面是代碼:

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 

謝謝我很欣賞它。

+0

好像你只是想intInner比intOuter ... – Joffan

回答

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 
+0

謝謝,@BrianMStafford我知道我很接近大我想我是看着這個TO0長,我的腦子在想它。我非常感謝幫助。 – Rico