Option Explicit '値のある最後のセルを選択するマクロ Sub MyLastCellSelect() Dim r As Range If TypeName(ActiveSheet) <> "Worksheet" Then MsgBox "ワークシートを選択して実行してください。", _ vbExclamation Exit Sub End If '値のある最後のセルを検索 (""を返す数式も対象にします) Set r = MyLastCell(oSheet:=ActiveSheet, bFormulas:=True) If r Is Nothing Then '値のあるセルがない場合はA1を選択 ActiveSheet.Range("A1").Select Else '値のある最後のセルを選択 r.Select End If End Sub '値のある最後のセルを検索する関数 'bFormulasがTrueのときは数式も対象にします。 'bFormulasがFalseのときは値を対象にします。""を返す数式や、 '表示形式等で非表示になっているセルは対象としません。 '定数の空文字列(')は常に対象としません。(Findで検索できないため。) '値のあるセルがない場合は、Nothingを返します。 Function MyLastCell(ByVal oSheet As Worksheet, _ ByVal bFormulas As Boolean) As Range Dim iLookin As Long Dim iRow As Long, iColumn As Long Dim oRange_Found As Range '検索対象の設定 If bFormulas Then iLookin = xlFormulas Else iLookin = xlValues End If '値のあるセルを最終セルから行方向に検索 Set oRange_Found = oSheet.Cells.Find(What:="*", _ After:=oSheet.Cells(1, 1), LookIn:=iLookin, _ LookAt:=xlWhole, SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, MatchCase:=False) '値のあるセルがない場合はNothingを返す If oRange_Found Is Nothing Then Set MyLastCell = Nothing Exit Function End If '最終行番号の取得 iRow = oRange_Found.Row '値のあるセルを最終セルから列方向に検索し、列番号を取得 iColumn = oSheet.Cells.Find(What:="*", _ After:=oSheet.Cells(1, 1), LookIn:=iLookin, _ LookAt:=xlWhole, SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, MatchCase:=False).Column '最後のセルを戻り値に設定 Set MyLastCell = oSheet.Cells(iRow, iColumn) End Function