VBA XLUP | ExcelでVBAXLUPを使用する方法は?(例付き)

Excel VBA XLUP

VBAコードを作成する際に留意する必要があることの1つは、通常のワークシートで行うことであり、VBAでも同じことを複製できます。VBAコーディングでのそのようなキーワードの1つは「XLUP」です。この記事では、このキーワードがVBAコーディングで何であるか、およびコーディングでどのように使用するかを示します。

コーディングでVBAXLUPを使用する方法は?

以下は、Excel VBAXLUPの例です。

このVBAXLUP Excelテンプレートはここからダウンロードできます– VBA XLUPExcelテンプレート

例1-セルをセルの削除された位置に移動する

たとえば、以下のデータのシナリオを見てください。ここでは、これらの色付きのセルデータを削除し、さらに下の行のデータを上のデータまで削除する必要があります。

ワークシートでこれを削除する1つの方法は、行全体を簡単に削除できるセルを選択することです。ただし、ここでは、表2の行も削除されても行全体を削除すると、表1のセルに色が付いているため、状況は少し注意が必要です。ただし、これを発生させたくないので、色付きの行を削除するだけで、下のセルを移動する必要があります。削除されたセルの位置を上げます。

まず、色付きのセルを選択し、Ctrl +マイナス記号(-)を押して[削除]オプションを開きます。

「削除」オプションを開くためのショートカットキー

「削除」オプションウィンドウには、4つのオプションがあり、要件に応じてアクションを選択できます。削除されたセルの位置に合わせてセルを上に移動する必要があるため、「ShiftCellUp」を選択します。

表2の行は変更されていません。

VBAでのこのアクションでは、VBAで同様のアクションのセットを実行するために、「XLUP」プロパティを使用する必要があります。次に、VBAエディターのウィンドウに移動し、マクロ名を開始します。

コード:

 Sub XLUP_Example()End Sub 

まず、この操作に含めるセルRANGEを指定します。このアクションでは、最初に削除されて上に移動するセルは「A5:B5」セルです。

コード:

 Sub XLUP_Example()Range( "A5:B5")End Sub 

この範囲のセルには、「削除」方法を選択します。

コード:

 Sub XLUP_Example()Range( "A5:B5")。EndSubを削除します 

「Delete」メソッドでわかるように、[Shift]として1つのオプションの引数があります。この引数には、「XLUP」として引数を入力する必要があります。

コード:

 Sub XLUP_Example()Range( "A5:B5")。Delete shift:= xlUp End Sub 

これで、このコードを手動で実行するか、ショートカットのExcelキーF5を使用して実行し、結果を確認できます。

表1に示すように、行番号6が5行目に移動しましたが、表では2行(色付き)が変更されていないため、「VBAXLUP」オプションを使用してこの操作を実行できます。

例2–XLUPを使用して最後に使用された行を検索する

A20番目のセル(下の画像を参照)にいて、最後に使用したセルがA14である状況を想像してみてください。

ここで、最後に使用したセル(A14)を選択する場合。ショートカットキーを使ってどうしますか?

私たちは、使用するCtrlキー+上矢印現在の位置から最後に使用セルに移動するキーを押します。

最後に使用したセルに移動するためのショートカットキー 

したがって、現在のセルから、Ctrl +上矢印で最後に使用したセルを選択しました。同様に、VBAコーディングでは、END(XLUP)を使用して同じことを実行します。

ここで、VBAコーディングウィンドウに戻ります。

In this window, we will perform the task of finding the last used row in the worksheet. Create a new subprocedure in the VBA window.

Code:

 Sub XLUP_Example1() End Sub 

To store the last used row number. define the variable as the VBA LONG data type.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long End Sub 

Now for this variable, we will assign the last used row number.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Last_Row_Number = End Sub 

Now use RANGE object and open this object.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Last_Row_Number = Range( End Sub 

Now mention the active cell (A20) for RANGE object.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20") End Sub 

Now open END property for supplied range cell.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End( End Sub 

As you can see above, we have to arrow key options like “xlDown”, “xlToLeft”, “xlToRight”, “xlUp”. Since we are moving up from the A14 cell choose the “VBA XLUP” option.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End(xlUp) End Sub 

After moving up from A14 cell we need to mention what we need to do since we need the last used row number I will use ROW property.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End(xlUp).Row End Sub 

Now for the message box assign the value of variable “Last_Row_Number”.

Code:

 Sub XLUP_Example1()  Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End(xlUp).Row MsgBox Last_Row_Number End Sub 

Now you can run this code manually or through shortcut key F5, to see the result.

So message box showing the last used row number as 14, so our last data used row number is A14 cell.

In this case, since data is very small we started from A20 cell but when the data is large we cannot say which cell to take into consideration first, in such cases we need to employ a different technique.

We need to use CELLS property, below is the example of the same.

Code:

 Sub XLUP_Example2() Dim Last_Row_Number As Long Last_Row_Number = Cells(Rows.Count, 1).End(xlUp).Row MsgBox Last_Row_Number End Sub 

Now you can run this code manually or through shortcut key F5, to see the result.

Instead of a RANGE object, I have used CELLS property. Let me explain this in detail to you.

ROW.COUNT this will count how many rows are there in column 1. What this will do is it will take into consideration of the last cell in the worksheet instead of random cell address, in the above case we have used A14 as the random cell address.

Things to Remember about VBA XLUP

  • XLUP is the word used in VBA code to replicate the action of the “Up Arrow” key in excel.
  • VBA XLUP is used to move from active cells to the above cell or last used cell.
  • XLUP is generally used along with END property in VBA.