Dictionary 配列の同じ要素を削除するDictionary(Bound)

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Sub DicArraySameElementDelBou(ByVal DB As VariantByRef DB2() As String)
'*****************************************************
'FSO 配列の同じ要素を削除するDictionary(Bound)
'*****************************************************

'Dictionaryオブジェクト
Dim obj As Object, i As Long, n As Long

  Set obj = CreateObject("Scripting.Dictionary")

  n = 0
  For i = LBound(DB) To UBound(DB)
      If obj.Exists(DB(i)) = False Then
        obj.Add DB(i), ""
        ReDim Preserve DB2(n) As String
        DB2(n) = DB(i)
        n = n + 1
      End If
  Next i

  Set obj = Nothing

End Sub


Private Sub test()
Dim i As Long, x(5) As String, DB2() As String
'テストデータ
x(0) = "1"
x(1) = "A"
x(2) = "1"
x(3) = "B"
x(4) = ""
x(5) = "1"

Call DicArraySameElementDelBou(x, DB2())

'値を表示
    For i = LBound(DB2) To UBound(DB2)
        Debug.Print i & vbTab & DB2(i)
    Next i

'0   1
'1   A
'2   B
'3
End Sub




 

2000年01月01日|[VBサンプルコード]:[Dictionary]