VBA basics
24. Input box (InputBox function)

Excel macro basics and applications, introduction to Excel VBA
last updated:08-28-2019

24. Input box (InputBox function)

Some of this topic was processed by machine translation. Original


Displays a message and text box in a dialog box,
You can get the string entered by the user.


In the beginning or middle of macro VBA, you may want to change the process according to user input.
To achieve this, the InputBox function is used in macro VBA.

InputBox function syntax

InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])



prompt Required.?String expression?displayed as the message in the dialog box. The maximum length of?prompt?is approximately 1024 characters, depending on the width of the characters used. If?prompt?consists of more than one line, you can separate the lines by using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or carriage return-linefeed character combination ((Chr(13) & (Chr(10)) between each line.
title Optional. String expression displayed in the title bar of the dialog box. If you omit?title, the application name is placed in the title bar.
default Optional. String expression displayed in the text box as the default response if no other input is provided. If you omit?default, the text box is displayed empty.
xpos Optional.?Numeric expression?that specifies, in twips, the horizontal distance of the left edge of the dialog box from the left edge of the screen. If?xpos?is omitted, the dialog box is horizontally centered.
ypos Optional. Numeric expression that specifies, in twips, the vertical distance of the upper edge of the dialog box from the top of the screen. If?ypos?is omitted, the dialog box is vertically positioned approximately one-third of the way down the screen.
helpfile Optional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If?helpfile?is provided,?context?must also be provided.
context Optional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If?context?is provided,?helpfile?must also be provided.

The argument can be a named argument.

Since helpfile and context are help, I will not use them very much, so I will omit explanation.

named argument.

An argument with a predefined name.
With named arguments, you can set the values in any order without having to specify the values for each argument in the order specified in the syntax.

* Named arguments will be explained in detail later.
25.About named arguments
An argument is a specification of a value passed when calling a function or method. There is a named argument as a description method when passing this argument. In the macro VBA description,: = is used.

However, in MsgBox, you don't often use named arguments, they are specified in order.

InputBox function example

Dim strIn As String
strIn = InputBox("Please enter something.")

VBA macro inputbox

When only a message is specified.
Check the displayed contents of the dialog screen.



Dim strIn As String
strIn = InputBox("Enter something." & vbLf & vbLf & "Write input sample here.")

VBA macro inputbox

When a multi-line message is specified.

Dim strIn As String
strIn = InputBox("Please enter something", "User input", "Default character")

VBA macro inputbox

When the message, title, and default value are used.
Check the displayed contents of the dialog screen.
What to do if nothing is entered or the “Cancel” button is pressed.

Dim strIn As String
strIn = InputBox("Please enter something", "User input", "Default character")
If strIn = "" Then
  MsgBox "Nothing was entered."
End If

If nothing is entered or the Cancel button is pressed, a blank is returned.
Therefore, the received variable can be judged with "".

About [, xpos] [, ypos]
Specify the display position.
Omitting it is not meaningful even if the sample is displayed.
However, it is usually displayed in the middle and is not specified.
Since the specified numerical value is twip, it will be difficult to manage with VBA, so it is omitted here.
For practical purposes, it should be displayed at the top left, in which case 0,0 should be specified.



Articles with the same theme "VBA basics"

21. Conditional branch (ElseIf)
22. Conditional branch (Select Case)
23. Message box (MsgBox function)
24. Input box (InputBox function)
25. About named arguments
26. General practice question 2
27. Select book / sheet (Select, Activate)
28. Select cell / row / column (Select, Activate)
29. Delete / Insert cell / row / column (Delete, Insert)
30. General practice question 3
31. Format Cells (display format, NumberFormatLocal)



  • >
  • >
  • 24. Input box (InputBox function)

  • If you find this site useful, please share and bookmark.


    I'm going to pay close attention to the description,
    If you have any mistakes or suggestions,「Contact Us」I hope you will let me know.
    Use the posted VBA code at your own risk.
    We are not liable for any damages such as data corruption.




    If you find this site useful, please share and bookmark.
    To the bottom of the text