Written by Allen Wyatt (last updated April 13, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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:
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 Microsoft 365.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...
Discover MoreIf you need to limit the cells that are accessible by the user of a worksheet, VBA can come to the rescue. This doesn't ...
Discover MoreMacros are often used to process information in a workbook. If your macro makes changes in what is selected in the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-01-16 13:16:28
J. Woolley
@Reg Pitt
You might consider a modeless UserForm, such as MsgBoxModeless described in my previous comment below. (Do not follow my earlier bad advice to Adrian.) When MsgBoxModeless is closed, it can run a followup procedure. Here is an example:
Sub MyMacro()
MsgBoxModeless "Waiting...", , , , ThisWorkbook.Name & "!MyNext"
End Sub
Sub MyNext()
MsgBox "Done"
End Sub
Generally you can't pause a macro waiting for a user to change a worksheet, but you can create a UserForm for this purpose.
On the other hand, consider Application.OnTime; see https://excelribbon.tips.net/T007223_Flashing_Cells.html
Or consider an event handler like Worksheet_Change; see https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.change
2023-01-15 12:43:57
Reg Pitt
I am a VBA beginner. I wrote an excel VBA code that sets some column headings and has selections in Named lists. the subroutine calls the dropdown list to a cell for the user to make a selection before he can procede to the next column. How do you get the macro to halt there, allow the selection to be made, and then continue on? I do not want the vba view code sheet to appear - I want the code sheet hidden and protected. If I use the Stop command, the macro will not start until I use F5 but it only works from the code sheet.
Thank you in advance
Reg
2020-12-24 11:16:37
J. Woolley
You might also be interested in the freely available MsgBoxModeless procedure included in My Excel Toolbox. See https://sites.google.com/view/MyExcelToolbox/
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments