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

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

create a new forum topic, and

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

Vishesh's picture

Funny thing - Font

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