VBAタイプステートメント(例)| VBAタイプで変数を宣言する方法は?

タイプは、DIM関数と同様の変数を定義するために使用されるVBAのステートメントであり、変数に1つ以上の値があるユーザー定義レベルで使用されます。ただし、タイプステートメントにはパブリックまたはプライベートの2つの命名法があります。これらはオプションで使用できますが、変数名と要素名は必須です。

Excel VBAのタイプステートメントとは何ですか?

VBA Type Statementは、各変数に異なるデータ型が割り当てられた1つのグループ名で変数を定義するために使用されます。これは、複数の変数を1つのオブジェクトの下にグループ化して、定義された型名で使用するのに役立ちます。

Typeステートメントを宣言することで、VBAでクラスモジュールを使用することを回避できます。スペースを節約できる既存のモジュールに埋め込むことができるため、文字列モジュールは必要ありません。

以前の記事の1つで、すべての変数を1つのグループ名でグループ化する「VBAENUM」について説明しました。

たとえば、「Mobiles」というグループ名がある場合、「Redmi、Oppo、Vivo、Samsung、LGなど」のようなグループメンバーがいます。したがって、Enumステートメントをそれぞれの値と一緒にグループ化できます。

列挙型モバイル

Redmi = 12000

Oppo = 18000

Vivo = 18000

サムスン= 25000

LG = 15000

列挙型の終了

このように、その記事で列挙型を作成しました。LONGデータ型しか保持できないため、Enumステートメントの問題。異なるデータ型の変数をグループ化するために、「VBATYPEステートメント」を使用できます。この記事では、VBAでTypeステートメントを作成する方法を紹介します。読む…

構文

Typeステートメントを使用して変数を宣言する前に、構文を確認してください。

タイプグループ名            [変数1]を変数データ型として            [変数2]を変数データ型として            [変数3]を変数データ型として            [変数4]を変数データ型として            [変数5]を変数データ型として終了タイプ

これらのタイプのステートメントは、モジュール内だけでなく、VBAのグローバル変数のようにモジュールの上部でも宣言できます。

VBAタイプはオブジェクト変数を保持でき、配列を保持できます。ただし、プロシージャ、関数を含めることはできません。

VBAでのタイプステートメントの例

このVBAタイプステートメントテンプレートはここからダウンロードできます–VBAタイプステートメントテンプレート

では、Typeステートメントで変数を宣言するプロセスを始めましょう。VBA列挙型で使用した方法と同じように、モバイルブランドを宣言する同じ例が表示されます。

ステップ1:モジュールの上部で「タイプ」という単語を開始し、グループのタイプに名前を付けます。

コード:

 タイプMobileBrandsエンドタイプ 

ステップ2:モバイルブランドでは、私たちが通常目にするものは何ですか。Nameが最初に表示されるので、変数をName asStringとして宣言します。

コード:

 タイプMobileBrandsName As String End Type 

ステップ3:名前の後に、発売日を確認します。変数をLaunchDateとしてDateとして宣言します。

コード:

 タイプMobileBrandsName As String LaunchDate As Date End Type 

ステップ4:次は、ストレージ容量を確認します。変数をストレージとして整数として宣言します。

コード:

 タイプMobileBrandsName As String LaunchDate As Date Storage As Integer End Type 

ステップ5:次はRAM容量をチェックします。

コード:

 タイプMobileBrandsName As String LaunchDate As Date Storage As RAM As Integer End Type 

ステップ6:最後に価格を確認します。

コード:

 タイプMobileBrandsName As String LaunchDate As Date Storage As Integer RAM As Integer Price As Long End Type 

サブプロシージャで、変数をタイプ名、つまりMobileBrandsとして宣言することにより、これらすべての変数データ型にアクセスできます。

Step 7: Create a subprocedure.

Code:

 Sub Type_Example1() End Sub 

Step 8: Now declare the variable “Mobile” as MobileBrnads.

Code:

 Sub Type_Example1() Dim Mobile As Mob End Sub 

Step 9: Now with the variable name “Mobile” we can access all the variables of “MobileBrands”.

Code:

Step 10: Now store each value like the below.

Code:

 Type MobileBrands Name As String LaunchDate As Date Storage As Integer RAM As Integer Price As Long End Type Sub Type_Example1() Dim Mobile As MobileBrands Mobile.Name = "Redmi" Mobile.LaunchDate = "10-Jan-2019" Mobile.Storage = 62 Mobile.RAM = 6 Mobile.Price = 16500 MsgBox Mobile.Name & vbNewLine & Mobile.LaunchDate & vbNewLine & _ Mobile.Storage & vbNewLine & Mobile.RAM & vbNewLine & Mobile.Price End Sub 

Finally, show the result in a VBA message box like the below one.

Code:

 Sub Type_Example1() Dim Mobile As MobileBrands Mobile.Name = "Redmi" Mobile.LaunchDate = "10-Jan-2019" Mobile.Storage = 62 Mobile.RAM = 6 Mobile.Price = 16500 MsgBox Mobile.Name & vbNewLine & Mobile.LaunchDate & vbNewLine & _ Mobile.Storage & vbNewLine & Mobile.RAM & vbNewLine & Mobile.Price End Sub 

Now run the code using F5 key or manually and see the result in a message box.

Like this, we can use the “VBA Type” statement to define new data type in the subprocedure.

VBA Types vs VBA Class

VBA Type often compared to VBA Class modules. There are certain differences between them. Below are the common differences.

  • Difference 1: VBA Type can contain only Public variables. VBA Class can contain both Public as well as Private variables.
  • Difference 2: VBA Type cannot contain Procedures and Function. VBA Class contains both of them along with properties.
  • Difference 3: VBA Type can be declared in any of the modules and procedures. VBA Class can only be declared in dedicated class modules.