27. VBA Tips - Inputbox

Nick's picture


A way to ask a user of your system a question is to use an Inputbox:

Here's what we're trying to achieve:

Ask the question:

inputbox

Display the answer:

inputbox

Here's the code:

inputbox

 

Explanation:

  1. InputBox takes several arguments:
    1. Prompt
    2. Title
    3. Default
    4. Xpos
    5. Ypos
    6. HelpFile
    7. Context
  2. In this example, we have entered:
    1. Prompt: "How Many Apples Do You Want?"
    2. Title: "Apples"
    3. Default: 3
  3. We then display the choice back to the user, but using this in a practical way, we'd trap the answer and use it for something else
  4. We check whether the return from the box is ""
    • If "" then they've pressed the Cancel button, and we should exit.

Download sheet to practise how to use the Inputbox in VBA

Training Video on how to use the Inputbox in VBA:

AttachmentSize
inputbox.xls48 KB

Input box in a userform

Hi Nick, first of all thank you for creating these blog and share your knowledge.

I have some work to do on Excel in a restaurant. I had created a file that shows the items menu, prices and some other stuff.
Unfortunately I am not an expert at all.
My Job is to create a user form that opens when open a special file;
It should welcome the user and ask if you are:
-a new user (then enable him to enter into the file)
-an existing user but want to enter/modify the months activity
-an existing user, willing to analyse the data in the main menu

In my opinion this is a tricky inputbox, I thought maybe you would know how to handle it.

Anyway thank you very much for your help and consideration.

Blackwater

Can't find anywhere, need help

I have created an INPUT Box however I want the input to be placed in a range in Excel, not have a message box appear.

I have looked everyhwere for this instruction...Can you help?

Nick's picture

inputbox

 

Range("A1").value = NumberOfApples

Funny thing

Thanks so much for your answer. I'm an excel expert but not a VBA wiz at all.

Funny thing happening when I do this....Depending on the number I place in the input box it will place a symbol in A1 like the forward symbol or maximze symbol?????

Sub InputBoxDemo()
NumberOfApples = InputBox("How Many Apples Do You Want?", "Apples", 3)
Range("A1").Value = NumberOfApples

End Sub

Vishesh's picture

Funny thing - Font

Check the font of cell A1 or any other thing like code or conditional formatting on that cell.

create a new forum topic, and

create a new forum topic, and add an example sheet with your problem.
The behaviour is v odd