|
[VB&VBA][Dictionary 配列の同じ要素を削除するDictionary(Bound)] |
Option Explicit
Sub DicArraySameElementDelBou(ByVal DB As Variant, ByRef 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
|
|
|
|
|
|
|