0% found this document useful (0 votes)
76 views

VBA InputBox - A Complete Guide

This document provides a guide to using the InputBox function in VBA. It discusses the differences between Application.InputBox and VBA.InputBox, describes the various parameters of InputBox including prompt, title, default, type, and how to get a range input. It also covers using optional parameters and creating enumerations for the type parameter to make the code more readable.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

VBA InputBox - A Complete Guide

This document provides a guide to using the InputBox function in VBA. It discusses the differences between Application.InputBox and VBA.InputBox, describes the various parameters of InputBox including prompt, title, default, type, and how to get a range input. It also covers using optional parameters and creating enumerations for the type parameter to make the code more readable.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

VBA Inputbox – A Complete

Guide

Contents [hide]
 1 Important
 2 InputBox Syntax
 3 InputBox Parameters
 4 VBA Optional Parameters
 5 InputBox Title Parameter
 6 InputBox Default Parameter
 7 InputBox Type Parameter Options
o 7.1 Getting the Range
 8 Related Reading
 9 What’s Next?
 10 Get the Free eBook

Important
Confusingly there are two InputBoxes in Excel VBA.

1. Application.InputBox
2. InputBox(also calledVBA.InputBox)

They are almost the same except that:

1. Application.InputBox allows you to specify the variable type of result e.g. String, integer,
date, range.
2. The Application.InputBox parameters Left and Top are not used by VBA.
In, the example below, the Application.InputBox allows you to specify the type but
the VBA.InputBoxdoesn’t:

number = VBA.InputBox("Enter Number")

number = Application.InputBox("Enter number", Type:=1) ' The type is


number

In this article, we will be dealing primarily with the Application.InputBox.

InputBox Syntax
InputBox prompt, title[optional], default [optional], left[optional], top[optional],
helpfile[optional], Left[optional]

InputBox Parameters
prompt – this is the text displayed by the InputBox e.g. “Please enter a number between one and
ten”, “Please select a range”.
title[optional] – this is the text that is displayed in the title bar of the InputBox.
default[optional]– this will be the response if no response is entered by the user.
left[optional] – not used. If you need to position the InputBox you need to use the
VBA.InputBox.
top[optional] – not used. If you need to position the InputBox you need to use the
VBA.InputBox.
helpfile[optional] – specifies a related help file if your application has one(hint: it probably
doesn’t unless it is a legacy application.)
helpfilecontextidl[optional] – specifies a position in the help file.
type[optional] – specifies the type of value that will be returned. If this parameter is not used
then the return type is text. See below for a list of options for this parameter.
What makes using the InputBox simple is that you really only need to use 4 of these parameters,
namely prompt, title, default and type.
VBA Optional Parameters
As, we saw in the above section, VBA has a lot of optional parameters. Sometimes we want to
use an optional parameter but don’t need the optional parameters before it. We can deal with this
in two ways:

1. Leave the other optional parameters blank.


2. Use the name of the parameter.

Here are examples of each method:

' Method 1: Using blank parameters

Number = Application.InputBox("Enter number", , 99)

Number = Application.InputBox("Enter number", , 99, , , , , 1)

' Method 2: Naming the parameters

Number = Application.InputBox("Enter number", Default:=99)

Number = Application.InputBox("Enter number", Default:=99, Type:=Number)

You can see that naming the parameters is a better idea as it makes the code much more readable
and understandable.

InputBox Title Parameter


The Title parameter simply allows you to see the Title of the InputBox dialog. The following
examples shows this:

Dim year As Long

year = Application.InputBox("Enter the Year", Title:="Customer Report")


InputBox Default Parameter
The default value is simply the value that will be returned if the user does not enter a value. This
value is displayed in the InputBox when it appears.
When the following code runs, the value Apple is displayed in the InputBox when it appears:

Dim fruit As Long

fruit = Application.InputBox("Please enter fruit", Default:="Apple")

InputBox Type Parameter Options


Value Type

0 Formula
Value Type

1 Number

2 String

4 Boolean - True or False

8 Range

16 An error value like #N/A

64 Array of values

You can create your own constants for the Type parameter if you want your code to be more
readable:

Public Enum appInputBox

IBFormula = 0

IBNumber = 1

IBString = 2

IBBoolean = 4

IBRange = 8

IBError = 16

IBArray = 64

End Enum

You can then use them like this:

year = Application.InputBox("Enter the Year", Type:=IBNumber)

year = Application.InputBox("Enter your name", Type:=IBString)


Getting the Range
To get a range from the user we set Type to 8.
If we set the return variable to be a range we must use the Set keyword like in this example:

Dim rg As Range

Set rg = Application.InputBox("Enter the Year", Type:=8)

If you leave out the Set keyword you will get the runtime error 91: “object variable or with block
not set”.

In VBA we can declare the variable as a variant in VBA. This means that VBA will set the type
at runtime:

' In both cases the variable will be a variant

Dim rg1 As Variant

Dim rg2

If we replace the Set keyword with a variant then the InputBox will return an array of values
instead of the range object:

Dim rg As Variant

' Returns an array of values

rg = Application.InputBox("Enter the Year", Type:=8)

' Returns the range object

Set rg = Application.InputBox("Enter the Year", Type:=8)

You might also like