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