VBA 크기 조정 | Excel VBA에서 크기 조정 속성을 사용하는 방법은 무엇입니까? (예제 포함)

Excel VBA 크기 조정

크기 조정은 필요에 따라 활성 셀에서 셀 범위를 변경하거나 크기를 조정하기 위해 VBA에서 사용할 수있는 속성입니다. 예를 들어 B5 셀에 있다고 가정하고이 셀에서 3 개의 행과 2 개의 열을 선택하려면 VBA의 RESIZE 속성을 사용하여 범위의 크기를 변경할 수 있습니다.

VBA 크기 조정 속성의 구문

다음은 VBA RESIZE 속성의 구문입니다.

Range (). Resize ([행 크기], [열 크기])

먼저 Range 개체 를 사용하여 크기를 조정할 셀을 제공해야 합니다.

그런 다음 Excel VBA Resize 속성을 사용 하고이 속성에서 행 크기 제한과 열 크기 제한 을 제공해야합니다 . 제공된 행 번호와 열 번호에 따라 크기가 조정됩니다.

VBA에서 크기 조정을 사용하는 예

다음은 Excel VBA에서 크기 조정을 사용하는 예입니다.

이 VBA 크기 조정 Excel 템플릿은 여기에서 다운로드 할 수 있습니다 – VBA 크기 조정 Excel 템플릿

예 1

A1에서 B14까지의 데이터가 있고 A1 셀의 데이터가 있다고 가정하면 Excel VBA의 RESIZE 속성을 사용하여 아래로 3 행과 왼쪽 범위 2 열을 선택하려면이 작업을 수행 할 수 있습니다.

다음은이 예에서 사용하는 데이터입니다.

따라서 먼저 RANGE 개체를 사용하여 첫 번째 셀 참조 또는 시작 지점을 제공해야합니다.이 예에서 시작 지점은 A1 셀입니다.

암호:

하위 Resize_Example () 범위 ( "A1"). End Sub

이 범위의 경우 RESIZE 속성을 사용하십시오.

암호:

 하위 Resize_Example () 범위 ( "A1"). Resize (End Sub 

RESIZE의 첫 번째 인수는 행 크기 이므로 데이터의 3 개 행을 선택하고 숫자 값 3을 제공해야합니다.

암호:

 하위 Resize_Example () 범위 ( "A1"). Resize (3, End Sub 

다음 인수는 열 크기 입니다. 을 선택하는 방법을 입력하고 3 개의 열을 입력합니다.

암호:

 Sub Resize_Example () 범위 ( "A1"). Resize (3,3) End Sub 

크기 조정이 완료되면이 범위로 수행 할 작업을 제공해야합니다. 시작하려면 "선택"방법을 선택하겠습니다.

암호:

 Sub Resize_Example () Range ( "A1"). Resize (3, 3) .End Sub 선택 

코드를 실행하고 선택할 행 수와 열 수를 확인합니다.

위의 A1 셀에서 볼 수 있듯이 아래로 3 개의 행과 오른쪽으로 3 개의 열을 선택했습니다.

예제 # 2

이제 아래 VBA 코드를 살펴보십시오.

위의 행 크기 코드에서는 빈 셀  을 제공 하고 열 크기 에는 3 을 제공했습니다 .

암호:

 Sub Resize_Example () Range ( "A1"). Resize (0, 3) .End Sub 선택 

코드를 실행하고 선택할 행 수와 열 수를 확인합니다.

보시다시피 활성 셀 행, 즉 첫 번째 행과 세 개의 열만 선택했습니다. 이는 행 크기에 대해 빈 셀 을 제공  하고 열 크기에 대해 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.