VBA Set Statemment | จะกำหนดค่าให้กับตัวแปรของวัตถุได้อย่างไร?

ชุดคำสั่ง Excel VBA

VBA Set เป็นคำสั่งที่ใช้ในการกำหนดคีย์ค่าใด ๆ ที่ระบุว่าวัตถุหรือการอ้างอิงถึงตัวแปรเราใช้ฟังก์ชันนี้เพื่อกำหนดพารามิเตอร์สำหรับตัวแปรบางตัวเช่นถ้าเราเขียน Set M = A ซึ่งหมายความว่าตอนนี้ M การอ้างอิงมีค่าและแอตทริบิวต์เหมือนกันกับที่ A มี

ใน VBA ออบเจ็กต์เป็นแกนหลักของ excel เพราะหากไม่มีวัตถุเราก็ไม่สามารถทำอะไรได้ วัตถุคือสมุดงานแผ่นงานและช่วง เมื่อเราประกาศตัวแปรเราจำเป็นต้องกำหนดประเภทข้อมูลให้กับมันและเรายังสามารถกำหนดออบเจ็กต์เป็นประเภทข้อมูลได้อีกด้วย ในการกำหนดค่าให้กับตัวแปรออบเจ็กต์ที่ประกาศเราต้องใช้คำว่า“ SET” คำว่า“ Set” ใช้เพื่ออ้างถึงออบเจ็กต์ใหม่ใน VBA เช่นหมายถึงช่วงเฉพาะของแผ่นงานนั้น ๆ

วิธีใช้ Excel VBA Set Statement

คุณสามารถดาวน์โหลดเทมเพลต VBA Set Statement ได้ที่นี่ - VBA Set Statement Template

# 1 - ตั้งค่าคำสั่งด้วยตัวแปร Range Object

ตัวอย่างเช่นสมมติว่าคุณต้องการใช้ช่วง A1 ถึง D5 ค่อนข้างบ่อย แทนที่จะเขียนโค้ดเป็น Range (“ A1: D5”) ทุกครั้งเราสามารถประกาศตัวแปรเป็น range และตั้งค่าการอ้างอิงช่วงเป็น Range (“ A1: D5”)

ขั้นตอนที่ 1:ประกาศตัวแปรเป็นวัตถุช่วง

รหัส:

 Sub Set_Example ()

Dim MyRange เป็นช่วง

End Sub

ขั้นตอนที่ 2:ช่วงเวลาที่เรากำหนดประเภทข้อมูลเป็นช่วงให้ใช้คำว่า "Set"

รหัส:

 Sub Set_Example () Dim MyRange As Range Set MyRange = End Sub 

ขั้นตอนที่ 3:พูดถึงช่วง

รหัส:

 Sub Set_Example () Dim MyRange As Range Set MyRange = Range ("A1: D5") End Sub 

ขั้นตอนที่ 4:ตอนนี้ตัวแปร“ MyRange” เท่ากับช่วง A1 ถึง D5 การใช้ตัวแปรนี้เราสามารถเข้าถึงคุณสมบัติและวิธีการทั้งหมดของช่วงนี้ได้

เราสามารถคัดลอกเพิ่มความคิดเห็นใน excel และทำสิ่งอื่น ๆ ได้อีกมากมาย

ตัวอย่างเช่นฉันได้สร้างตัวเลขไว้ที่นี่

ตอนนี้ใช้ตัวแปรฉันจะเปลี่ยนขนาดตัวอักษรเป็น 12

รหัส:

 Sub Set_Example () Dim MyRange As Range Set MyRange = Range ("A1: D5") MyRange.Font.Size = 12 End Sub 

การดำเนินการนี้จะเปลี่ยนขนาดแบบอักษรของช่วงที่กำหนด

ด้วยวิธีนี้เราสามารถทำหลายสิ่งกับช่วงหนึ่งโดยใช้คำว่า“ Set”

# 2 - ตั้งค่าคำสั่งด้วยตัวแปรของเวิร์กชีตอ็อบเจ็กต์

เราได้เห็นว่า "set" ทำงานอย่างไรกับออบเจ็กต์ช่วงใน VBA มันทำงานเหมือนกับวัตถุในแผ่นงานเช่นกัน

สมมติว่าคุณมี 5 เวิร์กชีตในเวิร์กบุ๊กของคุณและคุณต้องการกลับไปที่เวิร์กชีตหนึ่ง ๆ ต่อไปคุณสามารถตั้งชื่อเวิร์กชีตนั้นเป็นตัวแปรอ็อบเจ็กต์ที่กำหนดได้

ตัวอย่างเช่นดูโค้ดด้านล่าง

รหัส:

 Sub Set_Worksheet_Example () Dim Ws As Worksheet Set Ws = Worksheets ("Summary Sheet") End Sub 

ในโค้ดด้านบนตัวแปร“ Ws”ถูกกำหนดให้เป็นตัวแปรออบเจ็กต์และในบรรทัดถัดไปโดยใช้คำว่า“ Set” เราตั้งค่าตัวแปรเป็นเวิร์กชีตชื่อ“ เอกสารสรุป”

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

# 1 - ไม่มีคำ "ตั้งค่า"

รหัส:

 Sub Set_Worksheet_Example1 () 'เพื่อเลือกแผ่นงาน ("แผ่นงานสรุป") เลือก "เพื่อเปิดใช้งานแผ่นงานแผ่นงาน (" แผ่นงานสรุป ") เปิดใช้งาน" เพื่อซ่อนแผ่นงาน ("แผ่นงานสรุป") Visible = xlVeryHidden' ถึง ยกเลิกการซ่อนแผ่นงาน ("สรุปชีต") Visible = xlVisible End Sub 

ทุกครั้งที่ฉันใช้ออบเจ็กต์เวิร์กชีตเพื่ออ้างถึงชีต“ ชีตสรุป” ทำให้โค้ดมีความยาวมากและต้องใช้เวลาพิมพ์นานมาก

As part of the huge code, it is frustrating to type the worksheet name like this every time you need to reference the worksheet.

Now take a look at the advantage of using the word Set in Code.

#2 – With “Set” Word

Code:

 Sub Set_Worksheet_Example() Dim Ws As Worksheet Set Ws = Worksheets("Summary Sheet") 'To select the sheet Ws.Select 'To Activate the sheet Ws.Activate 'To hide the sheet Ws.Visible = xlVeryHidden 'To unhide the sheet Ws.Visible = xlVisible End Sub 

The moment we set the worksheet name we can see the variable name while entering the code as part of the list.

#3 – Set Statement with Workbook Object Variables

The real advantage of the word “Set” in VBA arises when we need to reference different workbooks.

When we work with different workbooks it is so hard to type in the full name of the workbook along with its file extension.

Assume you have two different workbooks named “Sales Summary File 2018.xlsx” and “Sales Summary File 2019.xlsx” we can set the two workbooks like the below code.

Code:

 Sub Set_Workbook_Example1() Dim Wb1 As Workbook Dim Wb2 As Workbook Set Wb1 = Workbooks("Sales Summary File 2018.xlsx") Set Wb2 = Workbooks("Sales Summary File 2019.xlsx") End Sub 

Now variable Wb1 is equal to the workbook named “Sales Summary File 2018.xlsx” and variable Wb2 is equal to the workbook named “Sales Summary File 2019.xlsx”.

Using this variable we can actually access all the properties and methods associated with the workbook.

We can shorten the code like the below.

Without Using Set Keyword to activate the workbook:

Workbooks("Sales Summary File 2018.xlsx").Activate

Using the Set Keyword to activate the workbook:

Wb1.Activate

This makes the writing of the code lot simpler and also once the workbook name is set there is a worry of typo error of the workbook names.