Return to VBA Code Examples

VBA InputBox – Get Input from a User

In this Article

  • The VBA Input Box with a Variable
    • The String Variable with an Input Box
    • The Number Variable with an Input Box
  • Go Input from a User
  • Returning the input to an Excel Canvas
  • Using VBA InputBox in Admission VBA
  • VBA Coding Made Like shooting fish in a barrel

This tutorial will demonstrate how to become input from a user with the VBA Input Box.

The VBA Input Box allows us to prompt the user to input information.  The information tin can then be used in our VBA Code or in an Excel worksheet.

The VBA Input Box with a Variable

We can declare a variable in our VBA code which then stores the information that is typed into the Input Box.  We tin can then employ this variable in our lawmaking.

Depending on the type of information nosotros desire returned from the input box, we tin can declare a specific type of variable to either render text (a string or variant variable) or to return a number (an integer, long, double or single variable).

The Cord Variable with an Input Box

Hither is how you can create an InputBox where the information is returned into a cord variable.

vba inputbox

Discover offset we declare a variable. This variable will exist assigned the value that the user enters.  We used variable type Cord in order for the Input box to accept text strings (which includes any alpha numeric characters).

The Number Variable with an Input Box

If you declare a number variable, you volition demand to enter a number but into the input box.

Showtime nosotros declare the number variable as an integer variable.   Nosotros can then likewise put a default value of 1 in the input box.

Inputbox integer

If we were to enter a string instead of a number and and then click on OK, nosotros would get a Type Mismatch Mistake as a number variable does not accept a string.

InputBox integer error

Become Input from a User

Hither is another example using the most popular features of an InputBox.

tiphacks3

The post-obit code does three things:

1. Prompts for input with an InputBox and assigns it to a variable

two. Verifies input, otherwise exits the sub

three. Returns the input in a message box

Returning the input to an Excel Sail

Yous can return the input that you type into an input box into a specific cell in your Excel canvas.

vba input box enter

We can also return input data to an Excel Canvass using a variable.

In the case higher up, we are required to enter a number.  If nosotros enter a number, so the variable dblAmount will put the number into the Excel canvass at prison cell A1.  All the same, if we do not enter a number, so a bulletin box will tell united states that nosotros did not enter a number and nothing will be put into prison cell A1.

InputBox Excel Error

Using VBA InputBox in Admission VBA

The VBA input box works exactly the aforementioned style in Access as it does in Excel when returning user input in the class of a message box.

Nevertheless, if you wish to return the user input to the database, yous would demand to use the Recordset object rather than the Range object as used in Excel.

vba inv no

VBA Coding Made Piece of cake

Stop searching for VBA code online. Learn more almost AutoMacro – A VBA Code Architect that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More than!


<<Render to VBA Examples