📊 Excel VBA Top100
🔧

InputFromUser

ユーティリティ

ユーザーから複数の値を入力받아シートに書き込む

🎬 デモGIF準備中

📖 使い方

  1. VBAエディタを開く(Alt + F11)
  2. モジュールを挿入(挿入 > モジュール)
  3. 下記VBAコードをコピー&ペースト
  4. ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: 実行すると入力項目数と各値を入力するダイアログが表示される

💻 VBAコード

' InputFromUser
' -----------------
' Macro Name: InputFromUser
' Description: ユーザーから複数の値を入力받아シートに書き込む
' Parameters: なし
' Returns: なし
' Usage: 実行すると入力項目数と各値を入力するダイアログが表示される
' -----------------

Sub InputFromUser()
    Dim ws As Worksheet
    Dim numInputs As Long
    Dim i As Long
    Dim promptText As String
    Dim userInput As String
    Dim inputValues() As String
    Dim startRow As Long
    Dim startCol As Long

    On Error GoTo ErrorHandler

    Set ws = ActiveSheet

    promptText = InputBox("入力する項目数を入力してください (1-20):", "ユーザー入力", "3")

    If promptText = "" Then
        Exit Sub
    End If

    If Not IsNumeric(promptText) Then
        MsgBox "数値を入力してください。", vbExclamation
        Exit Sub
    End If

    numInputs = CLng(promptText)

    If numInputs < 1 Or numInputs > 20 Then
        MsgBox "1から20の範囲で入力してください。", vbExclamation
        Exit Sub
    End If

    ReDim inputValues(1 To numInputs)

    For i = 1 To numInputs
        userInput = InputBox("値 " & i & " を入力:", "ユーザー入力")
        If userInput = "" Then
            MsgBox "キャンセルされました。", vbInformation
            Exit Sub
        End If
        inputValues(i) = userInput
    Next i

    startRow = InputBox("書き込み開始行を入力:", "セルの選択", "1")
    If startRow = "" Then startRow = "1"
    If Not IsNumeric(startRow) Then startRow = "1"

    startCol = InputBox("書き込み開始列を入力 (1=A, 2=B...):", "セルの選択", "1")
    If startCol = "" Then startCol = "1"
    If Not IsNumeric(startCol) Then startCol = "1"

    For i = 1 To numInputs
        ws.Cells(CLng(startRow) + i - 1, CLng(startCol)).Value = inputValues(i)
    Next i

    MsgBox numInputs & "件のデータを書き込みました。", vbInformation
    Exit Sub

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