最終行の下にデータを追加したい場合、一般的には空白の行を探して検索します。
しかし、上から検索していくと空白行がある場合はそこで止まってしまい、その下の行にデータがある場合も出てきます。
最終行を取得する場合はシートの一番下から上に向かって検索すると途中に空白行があっても関係なく最終行を取得できます。
●指定した列の最終行を取得するサンプル
'-----------指定した列の最終行を返す------------
Function search_last_row(ByVal col As Integer) As Integer
search_last_row = Cells(Rows.Count, col).End(xlUp).row
End Function
●列の範囲の最終行を取得する場合のサンプル
'---------------最終行を返す--------------
Function func_lastrow(ByVal startcol As Long, ByVal lastcol As Long) As Long
'項目列の最終行の最大値を取得
Dim tlast As Integer
Dim i As Integer
Dim maxrow As Integer
maxrow = 0
For i = startcol To lastcol
tlast = Cells(Rows.Count, i).End(xlUp).row
If maxrow < tlast Then
maxrow = tlast
End If
Next i
Debug.Print "最大行数=" & maxrow
'最終行を返す
func_lastrow = maxrow
End Function
コメント
コメントを投稿