LoginSignup
3
5

More than 5 years have passed since last update.

【VBA】Collectionオブジェクトの使い方

Last updated at Posted at 2016-12-21

概要

  • 普通の変数(Int, Stringなど)やオブジェクト変数を要素とする独自のオブジェクトを作成できる。
  • 連想配列として使える。(配列の方が速い)

使い方

宣言

Dim colTest As Collection
Set colTest = New Collection

または、

'非推奨
Dim colTest As New Collection

代入

Keyは重複不可であることに注意。

Dim number as Long
Dim arr(3) As String
dim hogeSheet as WorkSheet
number = 1
arr(2) = "2番目"

With colTest
        .Add Item:="アイテム1", Key:="i1"
        .Add Item:=number, Key:="数字" '数値でも
        .Add Item:=arr(), Key:="配列" '配列でも
        .Add Item:=hogeSheet, Key:="ワークシート" 'オブジェクトでも
End With

参照

num = colTest("数字") 'num = 1
str = colTest("配列")(2) 'str = "2番目"

参考

3
5
4

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
3
5