รายการ VBA วัตถุ | คำแนะนำเกี่ยวกับตาราง ListObject Excel ใน Excel VBA

ListObjects ใน VBA คืออะไร?

ในตารางโดยปกติสิ่งที่เราเห็นคือชุดข้อมูล แต่ในคำศัพท์ VBA ยังมีอีกมากมายเช่นมีช่วงของช่วงรายการข้อมูลทั้งหมดคอลัมน์นี้เรียกว่าคอลัมน์รายการและแถวเรียกว่าแถวรายการเป็นต้น ดังนั้นในการเข้าถึงคุณสมบัตินี้เรามีฟังก์ชัน inbuilt ที่เรียกว่า Listobjects และใช้กับฟังก์ชันแผ่นงาน

VBA ListObject เป็นวิธีการอ้างอิงถึงตาราง excel ในขณะที่เขียนโค้ด VBA ด้วยการใช้ VBA LISTOBJECTS เราสามารถสร้างลบตารางและทั้งหมดเราสามารถเล่นกับตาราง excel ในรหัส VBA ตาราง Excel เป็นเรื่องยากสำหรับผู้เริ่มต้นและแม้แต่ผู้ใช้ระดับกลางก็พบว่าการทำงานกับตารางทำได้ยาก เนื่องจากบทความนี้พูดถึงการอ้างอิงตาราง excel ในการเข้ารหัส VBA จึงเป็นการดีกว่าที่คุณจะมีความรู้เกี่ยวกับตารางใน excel

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

สร้างรูปแบบตารางโดยใช้ ListObjects ใน Excel VBA

ตัวอย่างเช่นดูข้อมูล excel ด้านล่าง

การใช้รหัส VBA ListObject เราจะสร้างรูปแบบตารางสำหรับข้อมูลนี้

คุณสามารถดาวน์โหลดเทมเพลต Excel VBA ListObjects ได้ที่นี่ - เทมเพลต Excel ของ VBA ListObjects
  • สำหรับข้อมูลนี้ก่อนอื่นเราต้องหาว่าแถว & คอลัมน์ที่ใช้ล่าสุดคืออะไรดังนั้นกำหนดตัวแปรสองตัวเพื่อค้นหาสิ่งนี้

รหัส:

 Sub List_Objects_Example1 () Dim LR As Long Dim LC As Long End Sub 

  • หากต้องการค้นหาแถวและคอลัมน์ที่ใช้ล่าสุดให้ใช้รหัสด้านล่าง

รหัส:

LR = เซลล์ (Rows.Count, 1). End (xlUp) .Row LC = เซลล์ (1, Columns.Count). End (xlToLeft) .Column

  • ตอนนี้กำหนดอีกหนึ่งตัวแปรเพื่อเก็บการอ้างอิงของข้อมูล

รหัส:

 Dim Rng เป็นช่วง 

  • ตอนนี้ตั้งค่าการอ้างอิงถึงตัวแปรนี้โดยใช้โค้ดด้านล่าง

รหัส:

 ตั้งค่า Rng = เซลล์ (1, 1) ปรับขนาด (LR, LC)

ตอนนี้เราต้องใช้เมธอด VBA“ ListObject.Add” เพื่อสร้างตารางและด้านล่างนี้เป็นไวยากรณ์ที่เหมือนกัน

ListObject.Add (ที่มา, XlListObjectHasHeaders, ปลายทาง, TableStyleName)

ที่มา:นี่ไม่ใช่ช่วงของเซลล์ที่เรากำลังแทรกตาราง ดังนั้นเราสามารถจัดหาอาร์กิวเมนต์สองตัวที่นี่คือ“ xlSrcRange”และ“ xlSrcExternal”

XlListObjectHasHeaders:ถ้าตารางที่แทรกข้อมูลมีส่วนหัวหรือไม่ ถ้าใช่เราสามารถระบุ“ xlYes”ได้หากไม่สามารถระบุ“ xlNo” ได้

ปลายทาง:นี่ไม่ใช่อะไรนอกจากช่วงข้อมูลของเรา

รูปแบบตาราง:หากคุณต้องการใช้สไตล์ตารางใด ๆ เราสามารถจัดเตรียมสไตล์ได้

  • ตกลงตอนนี้ในแผ่นงานที่ใช้งานอยู่เรากำลังสร้างตารางดังนั้นโค้ดด้านล่างจะสร้างตารางให้เรา

รหัส:

 Dim Ws เป็นชุดแผ่นงาน Ws = ActiveSheet Ws.ListObjects เพิ่ม xlSrcRange, xllistobjecthasheaders: = xl ใช่ปลายทาง: = Rng

  • หลังจากนี้เราต้องตั้งชื่อให้กับตารางนี้

รหัส:

Ws.ListObjects (1) .name = "EmpTable"

  • ด้านล่างนี้คือรหัสเต็มสำหรับการอ้างอิงของคุณ

รหัส:

 Sub List_Objects_Example1 () Dim LR As Long Dim LC As Long LR = Cells (Rows.Count, 1). End (xlUp) .Row LC = Cells (1, Columns.Count). End (xlToLeft) คอลัมน์ Dim Rng As Range ตั้งค่า Rng = Cells (1, 1) .Resize (LR, LC) Dim Ws As Worksheet Set Ws = ActiveSheet Ws.ListObjects.Add xlSrcRange, xllistobjecthasheaders: = xlYes, Destination: = Rng Ws.ListObjects (1) .name = " EmpTable "End Sub 

โอเคมารันโค้ดและดูมายากล

มันได้สร้างตารางข้อมูลดังกล่าวและได้รับชื่อตารางเป็น“EmpTable”

การจัดรูปแบบตาราง Excel ด้วย VBA ListObjects

เมื่อสร้างตาราง Excel แล้วเราสามารถทำงานกับตารางได้โดยใช้ vba ListObject collection

  • ขั้นแรกกำหนดตัวแปรเป็น“ ListObject”

รหัส:

 Sub List_Objects_Example2() Dim MyTable As ListObject End Sub 

  • Now set the reference to this variable by using the table name.

Code:

 Sub List_Objects_Example2() Dim MyTable As ListObject Set MyTable = ActiveSheet.ListObjects("EmpTable") End Sub 

Now the variable “MyTable” holds the reference for the table “EmpTable”.

  • Enter the variable name and put a dot to see the properties and methods of the VBA ListObject.

For example, if we want to select the entire table then we need to use the “Range” object and under this, we need to use the “Select” method.

Code:

MyTable.Range.Select

This would select the entire data table including the heading.

  • If you want to select only the contents of the table without headers then we need to use “DataBodyRange”.

Code:

MyTable.DataBodyRange.Select

Like this, we can play around with tables.

  • Below is the list of activity codes for your reference.

Code:

 Sub List_Objects_Example2() Dim MyTable As ListObject Set MyTable = ActiveSheet.ListObjects("EmpTable") MyTable.DataBodyRange.Select 'To Select data range without headers MyTable.Range.Select 'To Select data range with headers MyTable.HeaderRowRange.Select 'To Select table header rows MyTable.ListColumns(2).Range.Select 'To select column 2 including header MyTable.ListColumns(2).DataBodyRange.Select 'To select column 2 without header End Sub 

Like this, we can use the “ListObject” collection to play around with excel tables.

Things to Remember

  • VBA ListObject is the collection of objects to reference excel tables.
  • To access ListObject collection first we need to specify what worksheet we are referring to is.