ray88’s diary

お仕事で困ったとき用の自分用の覚書

最終行を取得するコード

①特定のセルから見てデータの入っている一番下の行を取得

 (途中う空白有の場合は使えない)

■ 最終行 = Range("B2").End.End(xlDown).Row

 

②エクセル全体の最終行を取得し、そこから順に上を見ていき、指定した列の

 データのある行を取得(途中空白ありもOK)

■ 最終行 = Cells(Rows.Count, 2).End(xlUp).Row

(使用例)

****************************************************************************

Function getLastRow(TargetColumn As Integer) As Integer    
    getLastRow = Cells(Rows.Count, TargetColumn).End(xlUp).Row
End Function
****************************************************************************
Sub test()
    Dim lastRow As Integer    
    lastRow = getLastRow(2)
    MsgBox lastRow  
End Sub
****************************************************************************