VBA 유형 문 (예제) | VBA 유형으로 변수를 선언하는 방법은 무엇입니까?

Type은 DIM 함수와 유사한 변수를 정의하는 데 사용되는 VBA의 문으로, 변수에 하나 이상의 값이있는 사용자 정의 수준에서 사용됩니다. 그러나 public 또는 private 인 type 문에는 두 가지 명명법이 있습니다. 이는 선택 사항이지만 변수 이름과 요소 이름이 필요합니다.

Excel VBA의 유형 문이란 무엇입니까?

VBA 유형 문은 각 변수에 다른 데이터 유형이 할당 된 단일 그룹 이름으로 변수를 정의하는 데 사용됩니다. 이렇게하면 정의 된 유형 이름으로 사용하기 위해 단일 개체 아래에 여러 변수를 그룹화 할 수 있습니다.

Type 문을 선언하면 VBA에서 클래스 모듈 사용을 피할 수 있습니다. 공간을 절약 할 수있는 기존 모듈에 포함 할 수 있기 때문에 문자열 모듈이 필요하지 않습니다.

이전 기사 중 하나에서 단일 그룹 이름으로 모든 변수를 그룹화하기 위해 "VBA ENUM"에 대해 설명했습니다.

예를 들어, "Mobiles"라는 그룹 이름이있는 경우 "Redmi, Oppo, Vivo, Samsung, LG 등"과 같은 그룹 구성원이 있습니다. 따라서 Enum 문은 각각의 값으로 그룹화 할 수 있습니다.

Enum 모바일

Redmi = 12000

Oppo = 18000

Vivo = 18000

삼성 = 25000

LG = 15000

End Enum

이와 같이 우리는 그 기사에서 열거 형을 만들었습니다. LONG 데이터 유형 만 보유 할 수 있으므로 Enum 문의 문제점. 데이터 유형이 다른 변수를 그룹화하기 위해“VBA TYPE Statement”를 사용할 수 있습니다. 이 기사에서는 VBA에서 Type 문을 생성하는 방법을 보여줍니다. 읽어…

통사론

Type 문을 사용하여 변수를 선언하기 전에 구문을 살펴보십시오.

유형 그룹 이름             [변수 1] 변수 데이터 유형으로             [변수 2] 변수 데이터 유형으로             [변수 3] 변수 데이터 유형으로             [변수 4] 변수 데이터 유형으로             [변수 5] 변수 데이터 유형으로 끝 유형

이러한 유형의 명령문은 VBA의 글로벌 변수와 같이 모듈 내 에서뿐만 아니라 모듈 상단에서도 선언 할 수 있습니다.

VBA 유형은 객체 변수를 보유 할 수 있으며 배열을 보유 할 수 있습니다. 그러나 절차, 기능을 포함 할 수 없습니다.

VBA의 형식 문 예

이 VBA 유형 문 템플릿은 여기에서 다운로드 할 수 있습니다 – VBA 유형 문 템플릿

이제 Type 문으로 변수를 선언하는 과정을 시작하겠습니다. VBA Enum에서 사용한 것과 같은 모바일 브랜드를 선언하는 동일한 예를 볼 수 있습니다.

1 단계 : 모듈 상단에서 "Type"이라는 단어를 시작하고 Type of group에 이름을 지정합니다.

암호:

 유형 MobileBrands 끝 유형 

2 단계 : 모바일 브랜드에서 우리가 일반적으로 보는 것은 무엇입니까? 먼저 Name이 표시되므로 변수를 Name으로 String으로 선언합니다.

암호:

 유형 MobileBrands 이름을 문자열 끝 유형으로 

3 단계 : 이름 뒤에 출시 날짜를 확인합니다. 변수를 LaunchDate as Date로 선언하십시오.

암호:

 Type MobileBrands Name As String LaunchDate As Date 종료 유형 

4 단계 : 다음으로 스토리지 용량을 확인합니다. 변수를 Storage로 Integer로 선언합니다.

암호:

 Type MobileBrands Name As String LaunchDate As Date Storage As Integer End Type 

5 단계 : 다음으로 RAM 용량을 확인합니다.

암호:

 Type MobileBrands Name As String LaunchDate As Date Storage As RAM As Integer End Type 

6 단계 : 마침내 가격을 확인합니다.

암호:

 Type MobileBrands 이름 As String LaunchDate As Date Storage As Integer RAM As Integer Price As Long End Type 

이제 Sub 프로 시저에서 변수를 유형 이름 즉 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.