VBA noob,如何在多个数组中挑出一个唯一值?

时间:2014-01-04 20:24:40

标签: arrays vba

我有9个数组,每个数组包含8个值:

all(1,1,0-8)
all(1,2,0-8)
.
.
.
all(3,2,0-8)
all(3,3,0-8)

我想获得驻留在其中一个数组中的唯一值,这意味着其他数组都没有。我怎么能这样做?

PS:我已经失去了10个小时,并且羞于分享我是如何将它们从彼此中提取出来的,但却发现它因某些原因无法正常工作,可能与奇数有关,所以我是菜鸟,放了10个小时努力已经。

i = 1                    'same box probablity elimination

c = 0
e = 0
k = 1
For i = 1 To 3
    For k = 1 To 3
      For c = 0 To 8
      e = 0
       For e = 0 To 8
         If k = 3 And i = 1 Then


            If all(i, k, e) - all(i, 1, c) = 0 Then
            all(i, k, e) = 0: all(i, 1, c) = 0



            End If
            If all(i, k, e) - all(i + 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 1, c) = 0
            End If
            If all(i, k, e) - all(i, 2, c) = 0 Then
            all(i, k, e) = 0: all(i, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 3, c) = 0
            End If
         End If

          If k = 3 And i = 2 Then
            If all(i, k, e) - all(i - 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i, 1, c) = 0 Then
            all(i, k, e) = 0: all(i, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i, 2, c) = 0 Then
            all(i, k, e) = 0: all(i, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 3, c) = 0
            End If
          End If

          If k = 3 And i = 3 Then
            If all(i, k, e) - all(i - 2, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i, 1, c) = 0 Then
            all(i, k, e) = 0: all(i, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i, 2, c) = 0 Then
            all(i, k, e) = 0: all(i, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 3, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 3, c) = 0
            End If
          End If

          If k = 2 And i = 1 Then
            If all(i, k, e) - all(i, 1, c) = 0 Then
            all(i, k, e) = 0: all(i, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 2, c) = 0
            End If
            If all(i, k, e) - all(i, 3, c) = 0 Then
            all(i, k, e) = 0: all(i, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 3, c) = 0
            End If
           End If

           If k = 2 And i = 2 Then
            If all(i, k, e) - all(i - 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i, 1, c) = 0 Then
            all(i, k, e) = 0: all(i, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i, 3, c) = 0 Then
            all(i, k, e) = 0: all(i, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 3, c) = 0
            End If
           End If

           If k = 2 And i = 3 Then
            If all(i, k, e) - all(i - 2, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i, 1, c) = 0 Then
            all(i, k, e) = 0: all(i, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 3, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i, 3, c) = 0 Then
            all(i, k, e) = 0: all(i, 3, c) = 0
            End If
           End If

           If k = 1 And i = 1 Then
            If all(i, k, e) - all(i + 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 1, c) = 0
            End If
                        If e = 1 And c = 1 And i = 1 And k = 1 Then MsgBox all(3, 1, 0) & all(3, 1, 1) & all(3, 1, 2) & all(3, 1, 3) & all(3, 1, 4) & all(3, 1, 5) & all(3, 1, 6)
                        If e = 1 And c = 1 And i = 1 And k = 1 Then MsgBox all(1, 1, 0) & all(1, 1, 1) & all(1, 1, 2) & all(1, 1, 3) & all(1, 1, 4) & all(1, 1, 5) & all(1, 1, 6)

            If all(i, k, e) - all(i + 2, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 1, c) = 0
                        If e = 1 And c = 1 And i = 1 And k = 1 Then MsgBox all(3, 1, 0) & all(3, 1, 1) & all(3, 1, 2) & all(3, 1, 3) & all(3, 1, 4) & all(3, 1, 5) & all(3, 1, 6)
                        If e = 1 And c = 1 And i = 1 And k = 1 Then MsgBox all(1, 1, 0) & all(1, 1, 1) & all(1, 1, 2) & all(1, 1, 3) & all(1, 1, 4) & all(1, 1, 5) & all(1, 1, 6)

            End If
            If all(i, k, e) - all(i, 2, c) = 0 Then
            all(i, k, e) = 0: all(i, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 2, c) = 0
            End If
            If all(i, k, e) - all(i, 3, c) = 0 Then
            all(i, k, e) = 0: all(i, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 3, c) = 0
            End If
           End If

           If k = 1 And i = 2 Then
            If all(i, k, e) - all(i - 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i, 2, c) = 0 Then
            all(i, k, e) = 0: all(i, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i, 3, c) = 0 Then
            all(i, k, e) = 0: all(i, 3, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 3, c) = 0
            End If
           End If

           If k = 1 And i = 3 Then
            If all(i, k, e) - all(i - 2, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 1, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 1, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 2, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i, 2, c) = 0 Then
            all(i, k, e) = 0: all(i, 2, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 3, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 3, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 3, c) = 0
            End If
            If all(i, k, e) - all(i, 3, c) = 0 Then
            all(i, k, e) = 0: all(i, 3, c) = 0
            End If
           End If
        Next e
      Next c
    Next k
Next i

i = 1                    'same box probablity elimination
c = 0
e = 0
k = 4
For i = 1 To 3
    For k = 4 To 6
      For c = 0 To 8
      e = 0
       For e = 0 To 8
         If k = 6 And i = 1 Then
            If all(i, k, e) - all(i, 4, c) = 0 Then
            all(i, k, e) = 0: all(i, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 4, c) = 0
            End If
            If all(i, k, e) - all(i, 5, c) = 0 Then
            all(i, k, e) = 0: all(i, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 2, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 6, c) = 0
            End If
         End If

          If k = 6 And i = 2 Then
            If all(i, k, e) - all(i - 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i, 4, c) = 0 Then
            all(i, k, e) = 0: all(i, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i, 5, c) = 0 Then
            all(i, k, e) = 0: all(i, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 6, c) = 0
            End If
          End If

          If k = 6 And i = 3 Then
            If all(i, k, e) - all(i - 2, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i, 4, c) = 0 Then
            all(i, k, e) = 0: all(i, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i, 5, c) = 0 Then
            all(i, k, e) = 0: all(i, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 6, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 6, c) = 0
            End If
          End If

          If k = 5 And i = 1 Then
            If all(i, k, e) - all(i, 4, c) = 0 Then
            all(i, k, e) = 0: all(i, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 5, c) = 0
            End If
            If all(i, k, e) - all(i, 6, c) = 0 Then
            all(i, k, e) = 0: all(i, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 6, c) = 0
            End If
           End If

           If k = 5 And i = 2 Then
            If all(i, k, e) - all(i - 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i, 4, c) = 0 Then
            all(i, k, e) = 0: all(i, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i, 6, c) = 0 Then
            all(i, k, e) = 0: all(i, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 6, c) = 0
            End If
           End If

           If k = 5 And i = 3 Then
            If all(i, k, e) - all(i - 2, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i, 4, c) = 0 Then
            all(i, k, e) = 0: all(i, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 2, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 2, 6, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i, 6, c) = 0 Then
            all(i, k, e) = 0: all(i, 6, c) = 0
            End If
           End If

           If k = 4 And i = 1 Then
            If all(i, k, e) - all(i + 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 4, c) = 0
            End If
            If all(i, k, e) - all(i, 5, c) = 0 Then
            all(i, k, e) = 0: all(i, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 5, c) = 0
            End If
            If all(i, k, e) - all(i, 6, c) = 0 Then
            all(i, k, e) = 0: all(i, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 2, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 2, 6, c) = 0
            End If
           End If

           If k = 4 And i = 2 Then
            If all(i, k, e) - all(i - 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 4, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 4, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i, 5, c) = 0 Then
            all(i, k, e) = 0: all(i, 5, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 5, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 5, c) = 0
            End If
            If all(i, k, e) - all(i - 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i - 1, 6, c) = 0
            End If
            If all(i, k, e) - all(i, 6, c) = 0 Then
            all(i, k, e) = 0: all(i, 6, c) = 0
            End If
            If all(i, k, e) - all(i + 1, 6, c) = 0 Then
            all(i, k, e) = 0: all(i + 1, 6, c) = 0
            End If
           End If

编辑:

我想到了一种算法,它可以处理那些数组中奇数个同时发生的值。我只能使用if-then,for-next和do-while,因此需要一些时间来实现它。

基本上它会将同意值存储在一个单独的变量中,并从阵列中一次性消除它们而不是在比较中。无论如何,感谢Floris的投入。

1 个答案:

答案 0 :(得分:1)

你可以尝试下面的变体(这是低效的,因为我创建了一个数组,其中一个元素为a中值的范围内的每个可能的整数;但它是一个开始...)

Sub uniqueEl()
Dim a
a = Array(Array(0, 0, 0, 0, 0, 0, 0, 0), _
                Array(1, 1, 1, 1, 1, 1, 1, 1), _
                Array(2, 2, 2, 2, 2, 2, 2, 2), _
                Array(3, 3, 3, 3, 3, 3, 3, 4))

Dim m1, m2
m1 = Application.WorksheetFunction.Min(a)
m2 = Application.WorksheetFunction.Max(a)
Dim b() As Integer
ReDim b(0 To m2 - m1)
Dim c, d
For Each c In a
  For Each d In c
    b(d - m1) = b(d - m1) + 1
  Next
Next

Dim ii
ii = m1
For Each c In b
  If c = 1 Then
    MsgBox "found the single element: it is " & ii
  End If
  ii = ii + 1
Next



End Sub