📊 Excel VBA Top100

AutoNumberRows

自動化

A列に行番号(1始まり)を自動入力します(ヘッダー行を除く)

🎬 デモGIF準備中

📖 使い方

  1. VBAエディタを開く(Alt + F11)
  2. モジュールを挿入(挿入 > モジュール)
  3. 下記VBAコードをコピー&ペースト
  4. ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: AutoNumberRows を実行すると、A2セルから最終データ行まで連番が入力されます

💻 VBAコード

' AutoNumberRows
' -----------------
' Macro Name: AutoNumberRows
' Description: A列に行番号(1始まり)を自動入力します(ヘッダー行を除く)
' Parameters: なし
' Returns: なし
' Usage: AutoNumberRows を実行すると、A2セルから最終データ行まで連番が入力されます
' -----------------

Sub AutoNumberRows()
    On Error GoTo ErrorHandler

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long

    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row

    If lastRow < 2 Then
        MsgBox "B列にデータが見つかりません", vbCritical, "エラー"
        Exit Sub
    End If

    For i = 2 To lastRow
        ws.Cells(i, 1).Value = i - 1
    Next i

    MsgBox (lastRow - 1) & " 行に番号を入力しました", vbInformation, "完了"

    Exit Sub

ErrorHandler:
    MsgBox "エラーが発生しました:" & vbCrLf & Err.Description, vbCritical, "エラー"
End Sub