📜  VBA-InputBox(1)

📅  最后修改于: 2023-12-03 15:20:58.079000             🧑  作者: Mango

VBA-InputBox

Introduction

VBA-InputBox is a powerful feature of Visual Basic for Applications (VBA) that allows programmers to prompt users for input within their applications. It provides a convenient way to gather information from the user during runtime, making applications more interactive and dynamic.

Usage

The syntax for using the InputBox function is as follows:

InputBox(prompt, [title], [default], [xpos], [ypos], [helpfile], [context])
Parameters
  • prompt (required): The prompt or message to be displayed in the input box.
  • title (optional): The title of the input box window.
  • default (optional): The default value to be displayed in the input box.
  • xpos (optional): The x-coordinate position of the input box window.
  • ypos (optional): The y-coordinate position of the input box window.
  • helpfile (optional): A help file to provide additional information.
  • context (optional): A numeric expression specifying the Help context ID.
Return Value

The InputBox function returns a string containing the input provided by the user. If the user clicks the Cancel button, the function returns an empty string.

Examples
Basic Input Prompt
Dim userResponse As String
userResponse = InputBox("Enter your name:", "Name Input")

This example displays an input box with the prompt "Enter your name:" and the title "Name Input". The user's response is stored in the userResponse variable.

Input with Default Value
Dim age As Integer
age = InputBox("Enter your age:", "Age Input", 25)

In this example, the input box displays the prompt "Enter your age:", the title "Age Input", and a default value of 25. The user can modify the default value or input a different age.

Specifying Position and Help Information
Dim input As String
input = InputBox("Enter your input:", "Input", , 100, 100, "helpfile.chm", 1000)

Here, the input box is positioned at coordinates (100, 100) on the screen. Additionally, a help file named "helpfile.chm" is specified, along with a Help context ID of 1000.

Conclusion

The VBA-InputBox feature is a valuable tool for gathering user input in VBA applications. By utilizing this function, programmers can create more interactive and user-friendly programs. Experiment with different options and parameters to make the most out of InputBox.