VBA: Input from a User in a Macro (InputBox)

August 14th, 2004 | Categories: VBA | Tags: , , ,

There are many instances when you may wish to prompt the user for Input from your macro. This is easily done by using an InputBox. The user Input can then be translated into a variable to use in your macros. Here is an example using the most popular features of an InputBox.

The following code does three things:
1. Prompts for input with an InputBox and assigns it to a variable
2. Verify’s input, otherwise exits
3. Returns the input in a message box

Public Sub MyInputBox()

Dim MyInput As String
    MyInput = InputBox("This is my InputBox", _
    "MyInputTitle", "Enter your input text HERE")

If MyInput = "Enter your input text HERE" Or _
MyInput = "" Then
Exit Sub
End If

MsgBox "The text from MyInputBox is " & MyInput

End Sub

Get LIVE Help
Custom Excel solutions & live Excel help!
► Go to www.ExcelAutomationHelp.com
► Email Now@ExcelAutomationHelp.com
  1. kapitan kosmos
    November 4th, 2009 at 10:49
    Reply | Quote | #1

    Thanks a lot – i was struggling with “formula error” on blank input box for a while now…

  2. sup bob
    May 7th, 2010 at 22:25
    Reply | Quote | #2

    even better is if u could make it so it could respond differently to different text
    not yes no questions

  3. earl
    July 2nd, 2010 at 18:29
    Reply | Quote | #3

    USELESS!!!! Why would I need a variable just in that sub? Why can’t it return anything so that you can do something useful with it in the main sub?!??

  4. peter
    April 1st, 2011 at 13:43
    Reply | Quote | #4

    How do i get a macro to do nothing if password is wrong, but execute rest of macro if true please

  5. 1 trackbacks