VBAのサイズ変更| Excel VBAでプロパティのサイズ変更を使用するにはどうすればよいですか?(例あり)

ExcelVBAのサイズ変更

サイズ変更は、必要に応じてアクティブセルからセルの範囲を変更またはサイズ変更するためにVBAで使用できるプロパティです。たとえば、セルB5にいると仮定します。このセルから、3行2列を選択する場合は、VBAのRESIZEプロパティを使用して範囲のサイズを変更できます。

VBARサイズ変更プロパティの構文

以下は、VBARESIZEプロパティの構文です。

Range()。Resize([行サイズ]、[列サイズ])

まず、Rangeオブジェクトを使用して、サイズを変更する必要のあるセルを指定する必要があります。

次に、Excel VBA Resizeプロパティを使用します。このプロパティでは、行サイズの制限と列サイズの制限を指定する必要があります。提供された行番号と列番号に基づいて、サイズが変更されます。

VBAでサイズ変更を使用する例

以下は、ExcelVBAでサイズ変更を使用する例です。

このVBARサイズ変更Excelテンプレートはここからダウンロードできます–VBAサイズ変更Excelテンプレート

例1

A1からB14セルまでのデータがあり、A1セルから下に3行、左に2列の範囲を選択する場合は、ExcelVBAのRESIZEプロパティを使用してこれを行うことができます。

以下は、この例で使用しているデータです。

したがって、最初に、RANGEオブジェクトを使用して、最初のセル参照または開始点を指定する必要があります。この例では、開始点はA1セルです。

コード:

Sub Resize_Example()範囲( "A1")。エンドサブ

この範囲には、RESIZEプロパティを使用します。

コード:

 Sub Resize_Example()Range( "A1")。Resize(End Sub 

RESIZEの最初の引数は行サイズであるため、データの3行を選択し、数値3を指定する必要があります。

コード:

 Sub Resize_Example()Range( "A1")。Resize(3、End Sub 

次の引数は、この列のサイズです。選択する必要のある列を入力します。3つの列を入力します。

コード:

 Sub Resize_Example()Range( "A1")。Resize(3,3)End Sub 

サイズ変更が完了したら、この範囲で行う必要があるものを提供する必要があります。まず、「選択」方法を選択します。

コード:

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

コードを実行して、選択する行数と列数を確認します。

上記のA1セルからわかるように、下に3行、右に3列が選択されています。

例2

次に、以下のVBAコードを見てください。

以下のための上記のコードでは行サイズ、我々は供給している空白のセルを とするために列のサイズ、我々は供給している3。

コード:

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

コードを実行して、選択する行数と列数を確認します。

ご覧のとおり、アクティブなセル行、つまり1行目と3列のみが選択されています。これは、行サイズには空白のセル を指定し、列サイズには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.