คำชี้แจงประเภท VBA (ตัวอย่าง) | จะประกาศตัวแปรด้วยประเภท VBA ได้อย่างไร?

Type เป็นคำสั่งใน VBA ซึ่งใช้เพื่อกำหนดตัวแปรที่คล้ายกับฟังก์ชัน DIM ซึ่งใช้ในระดับที่ผู้ใช้กำหนดโดยที่เรามีค่าตั้งแต่หนึ่งค่าขึ้นไปในตัวแปรมีสองระบบการตั้งชื่อสำหรับคำสั่ง type ซึ่งเป็นแบบสาธารณะหรือส่วนตัวอย่างไรก็ตาม สิ่งเหล่านี้เป็นทางเลือกที่จะใช้ แต่ต้องระบุชื่อตัวแปรและชื่อองค์ประกอบ

Type Statement ใน Excel VBA คืออะไร?

VBA Type Statement ใช้เพื่อกำหนดตัวแปรภายใต้ชื่อกลุ่มเดียวโดยมีประเภทข้อมูลต่างกันที่กำหนดให้กับตัวแปรแต่ละตัว สิ่งนี้ช่วยให้เราสามารถจัดกลุ่มตัวแปรหลายตัวเข้าด้วยกันภายใต้ออบเจ็กต์เดียวเพื่อใช้ภายใต้ชื่อประเภทที่กำหนด

ด้วยการประกาศคำสั่ง Type เราสามารถหลีกเลี่ยงการใช้โมดูลคลาสใน VBA ได้ ไม่จำเป็นต้องมีโมดูลสตริงใด ๆ เนื่องจากสามารถฝังลงในโมดูลที่มีอยู่แล้วซึ่งสามารถประหยัดพื้นที่ได้

ในบทความก่อนหน้านี้เราได้กล่าวถึง“ VBA ENUM” เพื่อจัดกลุ่มตัวแปรทั้งหมดเข้าด้วยกันภายใต้ชื่อกลุ่มเดียว

ตัวอย่างเช่นหากคุณมีชื่อกลุ่มที่เรียกว่า“ โทรศัพท์มือถือ” เรามีสมาชิกกลุ่มเช่น“ Redmi, Oppo, Vivo, Samsung, LG และอื่น ๆ .. ” ดังนั้นคำสั่ง Enum เราสามารถจัดกลุ่มเข้าด้วยกันตามค่าต่างๆ

โทรศัพท์มือถือEnum

Redmi = 12000

Oppo = 18000

Vivo = 18000

ซัมซุง = 25000

LG = 15000

สิ้นสุด Enum

เช่นนี้เราได้สร้างการแจงนับในบทความนั้น ปัญหาเกี่ยวกับคำสั่ง Enum เนื่องจากสามารถเก็บข้อมูลประเภท LONG ได้เท่านั้น ในการจัดกลุ่มตัวแปรที่มีประเภทข้อมูลต่างๆเข้าด้วยกันเราสามารถใช้“ VBA TYPE Statement” ในบทความนี้เราจะแสดงวิธีสร้างคำสั่ง Type ใน VBA อ่านต่อ…

ไวยากรณ์

ก่อนที่คุณจะประกาศตัวแปรโดยใช้คำสั่ง Type ให้ดูที่ไวยากรณ์:

พิมพ์ชื่อกลุ่ม            [ตัวแปร 1] เป็นชนิดข้อมูลตัวแปร            [ตัวแปร 2] เป็นชนิดข้อมูลตัวแปร            [ตัวแปร 3] เป็นชนิดข้อมูล            ตัวแปร [ตัวแปร 4] เป็นชนิดข้อมูล            ตัวแปร [ตัวแปร 5] เป็นชนิดข้อมูลตัวแปรประเภทสิ้นสุด

ประเภทของคำสั่งเหล่านี้สามารถประกาศได้ภายในโมดูลและที่ด้านบนของโมดูลเช่นตัวแปรทั่วโลกใน VBA

VBA Type สามารถเก็บตัวแปรออบเจ็กต์ได้สามารถเก็บอาร์เรย์ได้ อย่างไรก็ตามไม่สามารถมีขั้นตอนการทำงาน

พิมพ์คำชี้แจงตัวอย่างใน VBA

คุณสามารถดาวน์โหลดเทมเพลตคำชี้แจงประเภท VBA ได้ที่นี่ - เทมเพลตคำชี้แจงประเภท VBA

โอเคเริ่มกระบวนการประกาศตัวแปรด้วยคำสั่ง Type เราจะเห็นตัวอย่างเดียวกันของการประกาศแบรนด์มือถือเช่นวิธีที่เราใช้ใน VBA Enum

ขั้นตอนที่ 1:ที่ด้านบนของโมดูลให้เริ่มคำว่า "Type" และตั้งชื่อให้กับ Type of group

รหัส:

 พิมพ์ MobileBrands End Type 

ขั้นตอนที่ 2:ในแบรนด์มือถืออะไรคือสิ่งที่เรามักจะเห็น เราเห็นชื่อก่อนจึงประกาศตัวแปรเป็น Name as String

รหัส:

 พิมพ์ชื่อ MobileBrands เป็น String End Type 

ขั้นตอนที่ 3:หลังจากชื่อเราตรวจสอบวันที่เปิดตัว ประกาศตัวแปรเป็น LaunchDate เป็นวันที่

รหัส:

 พิมพ์ชื่อ MobileBrands เป็น String LaunchDate As Date End Type 

ขั้นตอนที่ 4:สิ่งต่อไปคือตรวจสอบความจุของพื้นที่จัดเก็บ เพื่อประกาศตัวแปรเป็น Storage เป็นจำนวนเต็ม

รหัส:

 พิมพ์ชื่อ MobileBrands เป็น String LaunchDate As Date Storage As Integer End Type 

ขั้นตอนที่ 5:สิ่งต่อไปคือตรวจสอบความจุ RAM

รหัส:

 พิมพ์ชื่อ MobileBrands เป็น String LaunchDate As Date Storage As RAM As Integer End Type 

ขั้นตอนที่ 6:ในที่สุดเราตรวจสอบเกี่ยวกับราคา

รหัส:

 พิมพ์ชื่อ MobileBrands เป็น String LaunchDate As Date Storage As Integer RAM As Integer Price As Long End Type 

ตอนนี้อยู่ในขั้นตอนย่อยโดยการประกาศตัวแปรเป็น Type Name เช่น 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.