VBA の最後の行と列を検索する

Glen Alfaro 2023年1月30日
  1. コード構文
  2. VBA の現在のワークシートのデータを含む最後の行/列を取得する
  3. VBA の別のワークシートからデータを含む最後の行/列を取得する
  4. 関数を使用してワークシートの最後の行/列を取得する
VBA の最後の行と列を検索する

スプレッドシートのデータを処理するときは、データの最後の行と最後の列を知る必要があります。カーソルが反復できる制限を設定すると便利です。

VBA には、スプレッドシートで使用された最後の行/列を返す組み込み関数がありません。ただし、単純な VBA ソリューションを使用すると、ワークシートで使用された最後の行/列の値を返すように設計されたユーザー定義関数を作成できます。

この記事では、Excel VBA を使用してスプレッドシートで使用されている最後の行と列を見つける方法を示します。

コード構文

最後の行を取得するコード:

Cells(Rows.Count,[colNum]).End(xlUp).Row

最後の列を取得するコード:

Cells(Rows.Count,[rowNum]).End(xlUp).Row

どこ、

[colNum] 最後の行が存在する列の整数値
[rowNum] 最後の列が存在する行の整数値

VBA の現在のワークシートのデータを含む最後の行/列を取得する

以下のコードブロックは、Microsoft Excel Objects のターゲットワークシートに書き込む必要があります。

Sub GetLastRowCol()

'Declare lastRow and Last Col as Long datatype as we need to presume the values will be _
assigned with these variables are very high numbers.
Dim lastRow as Long
Dim lastCol as Long

'Assigning lastRow and lastCol variable
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Debug.Print "The last Row is: " & lastRow & vbNewline & "The last Column is :" & lastCol 
End Sub

出力:

The last Row is :1
The last Column is :1

VBA の別のワークシートからデータを含む最後の行/列を取得する

複数のワークシートを扱う場合は、現在使用しているワークシートを明示的に割り当てる必要があります。

以下のコードブロックは、別のワークシートから最後に使用された行/列を取得する方法を示しています。

Sub GetLastRowCol()

'Declare lastRow and Last Col as Long datatype as we need to presume the values will be _
assigned with these variables are very high numbers.
Dim lastRow as Long
Dim lastCol as Long

'Declaring workbook and worksheets
Dim wb as Workbook
Dim s1, s2 as Worksheet

'Initializing the wb, s1, and s2 objects.
Set wb = ThisWorkBook
Set s1 = wb.Sheets("Sheet1")
Set s2 = wb.Sheets("Sheet2")

'Will return the row number of the last row in Column A on Sheet1
lastRow = s1.Cells(s1.Rows.Count, 1).End(xlUp).Row
'Will return the row number of the last row in Row 1 on Sheet1
lastCol = s1.Cells(1, s1.Columns.Count).End(xlToLeft).Column

Debug.print "The last row in Sheet1:" & lastRow & vbNewline & "The last column in Sheet 1:" & lastCol

'Will return the row number of the last row in Column A on Sheet2
lastRow = s2.Cells(s2.Rows.Count, 1).End(xlUp).Row
'Will return the row number of the last row in Column A on Sheet2
lastCol = s2.Cells(1, s2.Columns.Count).End(xlToLeft).Column

Debug.print "The last row in Sheet2:" & lastRow & vbNewline & "The last column in Sheet 2:" & lastCol

End Sub

出力:

The last row in Sheet1: 1
The last column in Sheet1: 1
The last row in Sheet2: 1
The last column in Sheet2: 1

関数を使用してワークシートの最後の行/列を取得する

以下の関数は、Worksheet 引数に基づいて最後の行または最後の列を返します。

Function GetLastRow(s As Worksheet) As Long
GetLastRow = s.Cells(s.Rows.Count, 1).End(xlUp).Row
End Function
Function GetLastCol(s As Worksheet) As Long
GetLastCol = s.Cells(1, s.Columns.Count).End(xlToLeft).Column
End Function

Sub testRow()
Dim wb As Workbook
Dim s1 As Worksheet

Set wb = ThisWorkbook
Set s1 = wb.Sheets("Sheet1")
Debug.Print GetLastRow(s1)
End Sub

Sub testCol()
Dim wb As Workbook
Dim s1 As Worksheet

Set wb = ThisWorkbook
Set s1 = wb.Sheets("Sheet1")
Debug.Print GetLastCol(s1)
End Sub

testRow 出力:

1

testCol 出力:

1

関連記事 - VBA Row

関連記事 - VBA Column