ปรับขนาด VBA | วิธีใช้ Resize Property ใน Excel VBA (พร้อมตัวอย่าง)

ปรับขนาด Excel VBA

Resize เป็นคุณสมบัติที่มีอยู่ใน VBA เพื่อเปลี่ยนหรือปรับขนาดช่วงของเซลล์จากเซลล์ที่ใช้งานอยู่ตามต้องการ ตัวอย่างเช่นสมมติว่าคุณอยู่ในเซลล์ B5 และจากเซลล์นี้หากคุณต้องการเลือก 3 แถวและ 2 คอลัมน์เราสามารถเปลี่ยนขนาดของช่วงได้โดยใช้คุณสมบัติ RESIZE ของ VBA

ไวยากรณ์ของคุณสมบัติการปรับขนาด VBA

ด้านล่างนี้คือไวยากรณ์ของคุณสมบัติ VBA RESIZE

ช่วง () ปรับขนาด ([ขนาดแถว], [ขนาดคอลัมน์])

ขั้นแรกเราต้องจัดหาเซลล์ที่เราต้องการปรับขนาดโดยใช้วัตถุRange

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

ตัวอย่างการใช้ Resize ใน VBA

ด้านล่างนี้คือตัวอย่างการใช้ resize ใน excel VBA

คุณสามารถดาวน์โหลด VBA Resize Excel Template ได้ที่นี่ - VBA Resize Excel Template

ตัวอย่าง # 1

สมมติว่าคุณมีข้อมูลจากเซลล์ A1 ถึง B14 และจากเซลล์ A1 หากคุณต้องการเลือก 3 แถวลงและสองคอลัมน์ทางซ้ายเราสามารถทำได้โดยใช้คุณสมบัติปรับขนาดใน Excel VBA

ด้านล่างนี้เป็นข้อมูลที่เราใช้สำหรับตัวอย่างนี้

ก่อนอื่นเราต้องจัดหาการอ้างอิงเซลล์แรกหรือจุดเริ่มต้นโดยใช้วัตถุ RANGE ในตัวอย่างนี้จุดเริ่มต้นคือเซลล์ A1

รหัส:

Sub Resize_Example () ช่วง ("A1") End Sub

สำหรับช่วงนี้ให้ใช้คุณสมบัติ RESIZE

รหัส:

 Sub Resize_Example () ช่วง ("A1") ปรับขนาด (End Sub 

อาร์กิวเมนต์แรกของ RESIZE คือRow Sizeดังนั้นเราต้องเลือก 3 แถวของข้อมูลและระบุค่าตัวเลขเป็น 3

รหัส:

 Sub Resize_Example () ช่วง ("A1") ปรับขนาด (3, End Sub 

อาร์กิวเมนต์ถัดไปคือขนาดคอลัมน์สำหรับสิ่งนี้ให้ป้อนว่าคุณต้องเลือกคอลัมน์อย่างไรฉันจะป้อน 3 คอลัมน์

รหัส:

 Sub Resize_Example () ช่วง ("A1") ปรับขนาด (3,3) End Sub 

เมื่อปรับขนาดเสร็จแล้วเราจำเป็นต้องจัดหาสิ่งที่เราต้องทำกับช่วงนี้ ฉันจะเลือกวิธี“ เลือก” เพื่อเริ่มต้น

รหัส:

 Sub Resize_Example () Range ("A1") ปรับขนาด (3, 3) เลือก End Sub 

เรียกใช้โค้ดและดูว่าจะเลือกกี่แถวและกี่คอลัมน์

ดังที่คุณเห็นด้านบนจากเซลล์ A1 มีการเลือกสามแถวลงและสามคอลัมน์ทางด้านขวา

ตัวอย่าง # 2

ตอนนี้ดูรหัส VBA ด้านล่าง

ในโค้ดด้านบนสำหรับRow Sizeเราได้ระบุเซลล์ว่าง และสำหรับColumn Sizeเราได้ให้3

รหัส:

 Sub Resize_Example () ช่วง ("A1") ปรับขนาด (0, 3) เลือก End Sub 

เรียกใช้โค้ดและดูว่าจะเลือกกี่แถวและกี่คอลัมน์

อย่างที่คุณเห็นได้เลือกเฉพาะแถวเซลล์ที่ใช้งานอยู่คือแถวที่ 1 และสามคอลัมน์ เนื่องจากสำหรับRow Sizeเราได้จัดเตรียมเซลล์ว่างไว้ และสำหรับColumn Sizeเราได้ให้ 3 ดังนั้นจึงได้เลือกช่วงข้อมูล

ตอนนี้ดูรหัสด้านล่าง

รหัส:

 Sub Resize_Example() Range("A1").Resize(3).Select End Sub 

What this code will do is it will select only three rows including the active cell row but no extra columns.

Example #3

Use Resize To Select Unknown Ranges. Resize is best utilized when you want to select an unknown range of cells. For example, look at the below image of the data range.

It has data all the ways from Column A to Column P and row-wise we have up until the 700th row.

Assume you know your data will keep changing and you want to select the data range every now and then by manually changing the row and column number. However, by using VBA RESIZE property we can do this easily.

Look at the below code.

Code:

 Sub Resize_Example1() Dim LR As Long Dim LC As Long Worksheets("Sales Data").Select LR = Cells(Rows.Count, 1).End(xlUp).Row LC = Cells(1, Columns.Count).End(xlToLeft).Column Cells(1, 1).Resize(LR, LC).Select End Sub 

First I have declared two variables to find the last used row (LR) and last used column (LC).

 Dim LR As Long Dim LC As Long 

Since our data is in the worksheet named “Sales Data” we are choosing this worksheet by using the below code.

Worksheets(“Sales Data”).Select

Now below code will find the last used row and last used column.

LR = Cells(Rows.Count, 1).End(xlUp).Row

LC = Cells(1, Columns.Count).End(xlToLeft).Column

Now from the first cell, we resizing the range from last used row to last used column and select is the method used. So now it doesn’t matter how big your data is it will dynamically select the data by finding the last used row and last used column.

Things to Remember

  • Resize property in VBA will change the size of the range from the active cell (including the active cell as well).
  • We just need to provide how many rows and how many columns to be resized from the active cell in VBA.
  • We cannot use negative row & column number for RESIZE property.