Application.InputBox メソッド (Excel) | Microsoft Learn

この記事では、ExcelのInputBoxメソッドの使い方について説明しています。このメソッドは、ユーザーからの入力を求めるダイアログボックスを表示し、入力された情報を返します。各パラメータ(Prompt, Title, Defaultなど)はオプションで、最大255文字のメッセージを表示できます。ユーザーがOKを選択すると入力値が返され、キャンセルの場合はFalseが返ります。また、Type引数を使うことで、返されるデータ型を指定でき、範囲オブジェクトや数式も扱えます。

VBAのInputBoxについて

VBA(Visual Basic for Applications)は、Excelを含むMicrosoft Office製品の自動化を可能にするプログラミング言語です。その中で、ユーザーからの入力を促すためにInputBox関数が用いられます。この記事では、InputBoxの基本的な使い方とその構文、パラメータ、および返り値について詳しく解説します。

InputBoxとは?

InputBoxは、ダイアログボックスを表示し、ユーザーからの入力を受け取るための機能です。このダイアログボックスには、入力値を送信するOKボタンとキャンセルするCancelボタンが含まれています。ユーザーがOKボタンを押すと、InputBoxは入力された値を返します。一方、Cancelボタンを押すと、Falseが返されます。

以下がInputBoxの構文です。

構文

expression.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)
  • expression: Applicationオブジェクトを表す変数。
  • Prompt: ダイアログボックスに表示されるメッセージ。必須です。
  • Title: 入力ボックスのタイトル。省略可能で、デフォルトは"Input"です。
  • Default: 初期表示される値。省略可能です。
  • Left: ダイアログボックスの表示位置(x座標)。省略可能です。
  • Top: ダイアログボックスの表示位置(y座標)。省略可能です。
  • HelpFile: ヘルプファイルの名前。省略可能です。
  • HelpContextID: ヘルプトピックのコンテキストID。省略可能です。
  • Type: 返されるデータの型。省略可能で、デフォルトはテキストです。

パラメータの詳細

名前 必須/省略可能 データ型 説明
Prompt 必須 String ダイアログボックスに表示されるメッセージ。最大255文字。
Title 省略可能 Variant 入力ボックスのタイトル。省略時はデフォルトのタイトルが使われます。
Default 省略可能 Variant ダイアログボックス表示時にテキストボックスに表示される値。省略時は空白です。
Left 省略可能 Variant 表示するダイアログボックスのx座標。
Top 省略可能 Variant 表示するダイアログボックスのy座標。
HelpFile 省略可能 Variant ヘルプファイルの名前。Helpボタンが表示される場合に使用します。
HelpContextID 省略可能 Variant ヘルプファイル内のトピックID番号。
Type 省略可能 Variant 返されるデータの型を指定します。

返り値

InputBoxは、指定されたType値に応じて異なるデータ型を返します。一般的には、ユーザーが入力したテキストが返されますが、Typeを指定することで数値や論理値、セル参照なども受け取ることができます。

使用例

数値の入力を促す例

以下の例では、ユーザーに数値を入力させるダイアログボックスを表示します。

myNum = Application.InputBox("Enter a number", Type:=1)

セルの選択を促す例

この例では、ユーザーにSheet1のセルを選択させ、選択したセルをRangeオブジェクトとして取得します。

Worksheets("Sheet1").Activate 
Set myCell = Application.InputBox("Select a cell", Type:=8)

値を掛け算する関数に渡す例

ユーザーが選択した範囲から三つの値を掛け算し、結果を返すユーザー定義関数に渡す例も示します。

Sub Cbm_Value_Select()
   Dim rng As Range
   Set rng = Application.InputBox("Range:", Type:=8)
   If rng.Cells.Count <> 3 Then
     MsgBox "Length, width and height are needed - please select three cells!"
      Exit Sub
   End If

   ActiveCell.Value = MyFunction(rng)
End Sub

Function MyFunction(rng As Range) As Double
   MyFunction = rng(1) * rng(2) * rng(3)
End Function

まとめ

VBAのInputBoxは、ユーザーからの入力を簡単に受け取るための非常に便利な機能です。この機能を使うことで、マクロやユーザー定義関数の利用がよりインタラクティブで柔軟になります。これを上手に活用して、エクセル作業をより効率的に行いましょう。

サポートとフィードバック

Office VBAやこのドキュメントに関する質問やフィードバックがある場合は、Office VBAのサポートとフィードバックをご覧ください。

————-

Application.InputBox method (Excel) | Microsoft Learn

Source link

The article discusses the use of the InputBox method in VBA (Visual Basic for Applications), specifically for Excel, which allows users to create a dialog box for inputting data. Here’s a summary of its key points:

Syntax and Parameters:

The basic syntax of the InputBox is:

expression.InputBox (Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)
  • Prompt (Required): The message displayed in the dialog box (max 255 characters).
  • Title (Optional): The title of the dialog box, defaults to "Input" if omitted.
  • Default (Optional): Initial value displayed in the text box.
  • Left & Top (Optional): X and Y positions for the dialog box on the screen.
  • HelpFile & HelpContextID (Optional): Provide help functionality if specified.
  • Type (Optional): Specifies the return data type, affecting how the entered data is returned.

Data Types:

The Type argument can accept various values:

  • 0: Formula
  • 1: Number
  • 2: Text (string)
  • 4: Logical value (True/False)
  • 8: Cell reference (as Range object)
  • 16: Error value (like #N/A)
  • 64: Array of values

Functionality:

  • The InputBox displays a dialog with "OK" and "Cancel" options. If "OK" is selected, it returns the user-inputted value; if "Cancel" is pressed, it returns False.
  • The method supports input validation and allows for Excel formulas and cell references.
  • InputBox can return different types based on the specified Type argument.

Examples:

  1. Basic Number Input:

    myNum = Application.InputBox("Enter a number")
  2. Cell Selection:

    Set myCell = Application.InputBox("Select a cell", Type:=8)
  3. Range Selection for a Function:
    The article illustrates using an InputBox to allow a user to select a range, confirming a three-cell selection before using that input in a custom function.

Conclusion:

The InputBox in VBA provides a straightforward way to gather user input efficiently within Excel, facilitating various data types and supporting direct interaction with Excel objects, formulas, and error values.

Support:

For additional help and feedback regarding Office VBA or its documentation, users are directed to refer to Microsoft’s support channels.

関連記事

コメント

この記事へのコメントはありません。