Pausing Macros for User Input

by Allen Wyatt
(last updated June 1, 2019)

7

For those who have been around spreadsheet programs for quite some time, you may remember the old {?}~ command that was available in Lotus 1-2-3. This command allows you to pause the macro while the user enters data in the spreadsheet.

Excel doesn't include the same capability, but it does have ways that you can prompt the user for input. The two primary methods are these:

  • MsgBox function. This function displays a dialog box and a set of buttons. When the user clicks on a button, an integer value is returned that indicates the button clicked. Your program can then take action based on the value returned. (For additional information on the MsgBox function, see this tip.)
  • InputBox function. This function displays a dialog box and allows the user to type a response. Whatever the user types is returned as a string to the macro. (For additional information on the InputBox function, see this tip.)

Both of these functions have been discussed in other issues of ExcelTips, as noted in the links above. Based on the user's input, you can modify what the macro does in any way desired. The only drawback to the functions is that they only return a single, discrete piece of data. In other words, they aren't designed to allow the user to input a range of cells and then continue processing. For instance, if you wanted to ask the user to provide five values destined for five cells, you would need to present an InputBox five times, depositing the user's responses into the desired cells one after the other.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9515) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Picking a Workbook Format

Need to share workbook information with a wide number of people? It can be puzzling to figure out which version of Excel ...

Discover More

Finding Unused Names

After months or years of naming things (such as cell ranges), you may find your workbook cluttered with a bunch of names ...

Discover More

Printing Color Separations with VBA

When printing in color (at a commercial printer) it is necessary to print different colors of your document in different ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Copying Pictures with a Macro

Copying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...

Discover More

Finding Workbooks Containing Macros

Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...

Discover More

Searching Very Large Strings in a Macro

VBA provides a few different ways you can search for information within strings. This tip looks at the most efficient ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 0?

2019-08-10 10:55:42

Willy Vanhaelen

@Adrian
The use of InputBox can be as simple as this:

Sub Test()
Range("A1") = InputBox("Type something")
' if needed, your code goes here
End Sub

If you enter a number, VBA enters it as numeric.
If you type a string, VBA enters it as text.
If you press OK without typing something or press Cancel or Esc, VBA will clear cell A1


2019-08-10 10:30:07

J. Woolley

@Adrian
I think you want a modeless UserForm. In VBE, use Insert > UserForm. Decorate your UserForm1 (add a Close button), then in your VBA macro, add:
Load UserForm1
UserForm1.Show 0 ' show modeless
Add a While loop with
Application.Wait(Now + TimeValue("0:00:01"))
to monitor the Close button's Click event. While the form is displayed, the macro will pause and you will be able to edit cells. When the form is closed, exit the While loop.


2019-08-09 10:17:18

Peter Atherton

Adrian

Sub tester()
Dim x As Variant, c As Range

x = Application.InputBox("Enter your text", "Something Useful", , Type:=2)
[a2] = x
For Each c In ActiveSheet.UsedRange
If c = [a2] Then
c.Font.Bold = True
End If
Next
End Sub

Of course you do not have to enter x into A2 the macro can just refer directly to x
If c = x then ...

Type 2 is a string, see J Jooleys post below.


2019-08-08 08:29:49

Adrian J Turner

I am sorry to appear very thick but I cannot work this out at all, using VBA is it possible to pause a macro to add text into cell a2 then hit return and move on and continue with the macro?


2019-06-05 11:42:18

Willy Vanhaelen

Here is a typical example of the use of InputBox and MsgBox: This macro asks to enter a column letter or letters and gives you the corresponding column number.

Sub ColNr()
X = InputBox("Column letter(s)")
X = Sheet1.Range(X & 1).Column
MsgBox "Column number is: " & X
End Sub

This is a simple macro that does not trap errors.
You can even combine it's 3 lines to one line:

Sub ColNr()
MsgBox "Column number is: " & Sheet1.Range(InputBox("Column letter(s)") & 1).Column
End Sub

By adding On Error Resume Next you trap all errors. You don't get an answer but neither do you get a crash message from vba:

Sub ColNr()
On Error Resume Next
MsgBox "Column number is: " & Sheet1.Range(InputBox("Column letter(s)") & 1).Column
End Sub

Here is something to play with:
Copy the following line to the clipboard:
MsgBox "Column number is: " & Sheet1.Range(InputBox("Column letter(s)") & 1).Column
paste it into the Immediate Window in vba and press enter. Have fun.


2019-06-04 13:21:52

JMJ

@J. Woolley: VERY interesting! I didn't know that form of InputBox, but it is the answer to many, many problems... Thank you!


2019-06-01 10:46:53

J. Woolley

Application.InputBox is more versatile than VBA.InputBox (which only returns a String). Application.InputBox can return a Range or an array of values. See
https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.