LoginSignup
5
12

More than 5 years have passed since last update.

Excel VBAで使える可変長なコレクションまとめ

Last updated at Posted at 2019-01-06

概要

Excel VBAには、Collectionという可変長なコレクションが組み込まれています。
それに加えて.NET Frameworkのコレクションも使用できます。
それぞれで微妙に違っているので整理しました。

一覧

※ 一部でDebug.Printを使った例を示してます。

Collection Dictionary ArrayList SortedList
生成 Dim c As Collection
Set c = New Collection
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
Dim arrayList
Set arrayList = CreateObject("System.Collections.ArrayList")
Dim sortedList
Set sortedList = CreateObject("System.Collections.SortedList")
追加 Call c.Add("Value1", "Key1") ' 注意:Collectionと順番が違う
Call dict.Add("Key1", "Value1")
Call arrayList.Add("Value1") ' 注意:Collectionと順番が違う
Call sortedList.Add("Key1", "Value1")
参照(byインデックス) ' 1スタート
c.Item(1)
' 0スタート
dict.Items()(0)
' 0スタート
arrayList.Item(0)
' 0スタート
sortedList.GetByIndex(0)
参照(byキー) c.Item("Key1") dict.Item("Key1") 無し sortedList.GetByIndex(sortedList.IndexOfKey("Key1"))
キーの参照 無し ' 0スタート
dict.Keys()(0)
無し ' 0スタート
sortedList.GetKey(0)
削除(byインデックス) ' 1スタート
Call c.Remove(1)
' 0スタート
Call dict.Remove(dict.Keys()(0))
' 0スタート
Call arrayList.RemoveAt(0)
' 0スタート
Call sortedList.RemoveAt(0)
削除(byキー) Call c.Remove("Key1") Call dict.Remove("Key1") 無し Call sortedList.Remove("Key1")
要素数 c.Count dict.Count arrayList.Count sortedList.Count
For(全Key) 無し Dim tmpKey
For Each tmpKey In dict.Keys
    Debug.Print tmpKey
Next
無し Dim i As Long
For i = 0 To sortedList.Count - 1
    Debug.Print sortedList.GetKey(i)
Next
For(全Value) Dim tmp
For Each tmp in c
    Debug.Print tmp
Next
Dim tmpValue
For Each tmpValue In dict.Items
    Debug.Print tmpValue
Next
Dim tmp
For Each tmp In arrayList
    Debug.Print tmp
Next
Dim i As Long
For i = 0 To sortedList.Count - 1
    Debug.Print sortedList.GetByIndex(i)
Next
キーの存在チェック ' 無いけど欲しいので自作
Function ExistsCollection(ByRef c As Collection, ByRef key) As Boolean
    On Error Resume Next
    Call c.Item(key)
    If Err.Number <> 0 Then
        ExistsCollection = False
    Else
        ExistsCollection = True
    End If
    On Error GoTo 0
End Function

' 使い方
ExistsCollection(c, "Key1")
dict.Exists("Key1") 無し sortedList.ContainsKey("Key1")
値の存在チェック 無し 無し arrayList.Contains("Value1") sortedList.ContainsValue("Value1")
全削除 ' 全削除は無いので新規作成する。
' 他で参照のコピーを持っている場合、そっちはクリアされない
Set c = New Collection
dict.RemoveAll arrayList.Clear sortedList.Clear

備考

CreateObjectで
「実行時エラー '-2146232576(80131700)':オートメーションエラーです。」
のエラーが出る場合があります。
image.png

その場合は「Windowsの機能の有効化または無効化」から「.NET Framework 3.5 (.NET 2.0および3.0を含む)」を有効化してください。
image.png

リンク

Dictionary:https://msdn.microsoft.com/ja-jp/library/cc428065.aspx
ArrayList:https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.arraylist?view=netframework-4.7.2
SortedList:https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.sortedlist

5
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
12