2011-06-23 21 views
0

私は、データベース(prop1,2,3)で最大3つの異なるプロパティを同時に検索できる検索機能を作成しています。結果を入力してVBAでこのサブを作成しました検索された小道具を配列に変換します。しかし、3つまでの配列を持つようになったので、配列に重複しているデータだけが結果に表示されるように配列を統合する必要があります。 1)ユーザーが検索しているプロパティの配列だけを見て、2)結果の範囲に表示できるように、最終的な配列に繰り返されるデータのみを取る方法に関するアドバイスはありますか?どんな助けでも大歓迎です!ありがとう!VBA ::配列の整理

+0

コードの一部と試した内容を表示できますか?あなたのコードの終わりの前にあなたの配列をマージするかもしれない – JMax

答えて

0

エントリがデータベースから直接であるため、一つの特性のために一意である、私はシンプルなソリューションのための手順を以下の考えることができると仮定すると:

  1. 、(一緒にPROP1PROP2を配列をマージしますこの例のコードでprop3>TEMP)各要素の
  2. カウント出現(tempCount
  3. 発生についての知識に基づいて、私は、コードにこれを追加いくつかのサンプルを確認するには、最終的な配列(ここで結果と呼ぶ)

    Dim prop1() As Variant 
    Dim prop2() As Variant 
    Dim prop3() As Variant 
    Dim temp() As Variant 
    Dim tempCount() As Integer 
    Dim result() As Variant 
    
    ReDim temp(UBound(prop1) + UBound(prop2) + UBound(prop3) + 1) 
    
    'merge arrays 
    Dim i As Integer 
    On Error Resume Next 
        For i = 0 To UBound(temp) 
        temp(i * 3) = prop1(i) 
        temp(i * 3 + 1) = prop2(i) 
        temp(i * 3 + 2) = prop3(i) 
    Next i 
    
    'count occurences 
    ReDim tempCount(UBound(temp) + 1) 
    Dim j As Integer 
    For i = 0 To UBound(temp) 
    tempCount(i) = 1 
    For j = 0 To i - 1 
    
    'comparison of elements 
        If temp(i) = temp(j) Then 
        tempCount(i) = tempCount(i) + 1 
        End If 
    Next j 
    Next i 
    
    ReDim result(UBound(temp) + 1) 
    
    'if an element occurs 3 times, add it to result 
    Dim count As Integer 
    count = 0 
    For i = 0 To UBound(tempCount) 
        If tempCount(i) = 3 Then 
         result(count) = temp(i) 
         count = count + 1 
        End If 
    Next i 
    

を作成します。 tempCountだけの時点で発生累積数を保持している:それは単にTEMP結果tempCountは列A、BおよびCに

'some sample arrays 
prop1 = Array("a", "b", "c", "d", "e") 
prop2 = Array("b", "c", "f") 
prop3 = Array("b", "c", "d", "g") 

'some sample Output 

'temp 
Cells(1, 1).Value = "temp:" 
For i = 0 To UBound(temp) 
    Cells(i + 2, 1).Value = temp(i) 
Next i 

'result 
Cells(1, 2).Value = "result:" 
For i = 0 To UBound(result) 
    Cells(i + 2, 2).Value = result(i) 
Next i 

'count: 
Cells(1, 3).Value = "count:" 
For i = 0 To UBound(tempCount) 
    Cells(i + 2, 3).Value = tempCount(i) 
Next i 

ノートアレイをプリントアウト要素はで監視されています。