行番号を返す
「この値がある行番号を知りたい」「見出しの行番号を取りたい」「範囲の最終行番号を返したい」——初心者でも迷わず使える最短コードと安全テンプレをまとめました。
基本:行番号をそのまま返す
Sub RowBasic()
Dim r As Long
r = Range("C4").Row 'セルC4の行番号→4
MsgBox r
End Sub
VB- ポイント:
- Range.Row: 指定セルの行番号を返す(A=1行目、B=1列目などの絶対位置)。
- 選択中の行番号: ActiveCell.Row、Selection.Row(範囲なら左上セルの行番号)。
最短で「値のある行番号」を返す(Find)
Sub RowByValue_Find()
Dim src As Range, hit As Range, rowNo As Long
Set src = Range("A2:A1000")
Set hit = src.Find(What:="顧客1001", LookAt:=xlWhole, LookIn:=xlValues)
If Not hit Is Nothing Then
rowNo = hit.Row
MsgBox "行番号: " & rowNo
Else
MsgBox "見つかりません"
End If
End Sub
VB- ポイント:
- 完全一致: LookAt:=xlWhole。部分一致なら xlPart。
- 行番号の使いどころ: Cells(rowNo, “H”) のように同じ行の別列へアクセス。
例外に強い:MATCHで「位置」→行番号に換算
Sub RowByValue_Match()
Dim pos As Variant, rowNo As Long, look As Range
Set look = Range("A2:A1000")
On Error Resume Next
pos = Application.WorksheetFunction.Match("顧客1001", look, 0) '0=完全一致
On Error GoTo 0
If Not IsError(pos) Then
rowNo = look.Row + CLng(pos) - 1
MsgBox "行番号: " & rowNo
Else
MsgBox "見つかりません"
End If
End Sub
VB- ポイント:
- MATCHは「範囲内の位置」を返す。開始行を足して「実行番号」に変換。
見出しが縦に並ぶ「行名」から行番号を返す
Function GetRowByName(ByVal rowName As String, ByVal nameRange As Range, _
Optional ByVal partial As Boolean = False, _
Optional ByVal matchCase As Boolean = False) As Long
Dim hit As Range, lookAt As XlLookAt
lookAt = IIf(partial, xlPart, xlWhole)
Set hit = nameRange.Find(What:=rowName, LookAt:=lookAt, LookIn:=xlValues, MatchCase:=matchCase)
If Not hit Is Nothing Then GetRowByName = hit.Row Else GetRowByName = 0
End Function
Sub Example_GetRowByName()
Dim r As Long
r = GetRowByName("売上合計", Range("A1:A2000"))
If r > 0 Then Cells(r, "D").Value = "ここに結果"
End Sub
VB- ポイント:
- 未検出は0返し: 呼び出し側で安全に分岐できる。
範囲の最初・最後の行番号を返す(最終行テンプレ)
Sub FirstLastRowInRange()
Dim rg As Range: Set rg = Range("A1").CurrentRegion
Dim firstRow As Long, lastRow As Long
firstRow = rg.Row
lastRow = rg.Row + rg.Rows.Count - 1
MsgBox "先頭=" & firstRow & ", 最終=" & lastRow
End Sub
VB- ポイント:
- CurrentRegion: 見出しから繋がった表の塊を自動取得。
- 最終行: Row + Rows.Count – 1 が定番。
大量データを爆速で「前方一致で行番号を収集」(配列+辞書)
Sub RowByPrefix_ArrayDict()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim src As Range: Set src = Range("A2:A200000")
Dim v As Variant: v = src.Value
Dim prefix As String: prefix = "ORD"
Dim rowsHit As Object: Set rowsHit = CreateObject("Scripting.Dictionary")
Dim i As Long, s As String
For i = 1 To UBound(v, 1)
s = CStr(v(i, 1))
If Left$(UCase$(s), Len(prefix)) = UCase$(prefix) Then
rowsHit(src.Row + i - 1) = True '実際の行番号をキーに保持
End If
Next
If rowsHit.Count > 0 Then
Dim k As Variant
For Each k In rowsHit.Keys
Cells(k, "A").Font.Bold = True
Next
End If
Cleanup:
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
VB- ポイント:
- セル往復を避ける: 範囲→配列で高速。辞書に「実行番号」を溜めて使い回せる。
よくある落とし穴と対策
- 未一致でエラー停止(MATCH・Find設定の引き継ぎ)
- 対策: MATCHは On Error で守る。Findは LookAt/LookIn/MatchCase を毎回明示。
- 「位置」と「行番号」の取り違え
- 対策: MATCH結果は位置。開始行を足して行番号に換算。
- 全角半角・大小文字の揺れで一致しない
- 対策: 比較前に UCase や StrConv(vbNarrow) で統一。
- 最終行の取得が不安定
- 対策: CurrentRegionで塊を取るか、列ごとの最終行を使う(Cells(Rows.Count, col).End(xlUp).Row)。
例題で練習
'例1:A列で「商品B」をFind→行番号を使ってH列に計算結果
Sub Example_FindRowCalc()
Dim hit As Range, r As Long
Set hit = Range("A2:A10000").Find(What:="商品B", LookAt:=xlWhole, LookIn:=xlValues)
If Not hit Is Nothing Then
r = hit.Row
Cells(r, "H").Value = Val(Cells(r, "C").Value) * Val(Cells(r, "D").Value)
End If
End Sub
'例2:MATCHで行番号に換算して、その行を選択
Sub Example_MatchSelectRow()
Dim pos As Variant, rowNo As Long, look As Range
Set look = Range("A2:A1000")
On Error Resume Next
pos = Application.WorksheetFunction.Match("顧客1001", look, 0)
On Error GoTo 0
If Not IsError(pos) Then
rowNo = look.Row + CLng(pos) - 1
Rows(rowNo).Select
End If
End Sub
'例3:範囲の最終行番号を返して見出し+データの高さを表示
Sub Example_LastRowOfTable()
Dim rg As Range: Set rg = Range("A1").CurrentRegion
Dim firstRow As Long, lastRow As Long
firstRow = rg.Row: lastRow = rg.Row + rg.Rows.Count - 1
MsgBox "表は " & firstRow & " ~ " & lastRow & " 行"
End Sub
VB