12.8 MsgBox and InputBox
Sub AskCity()
Dim city As String
Dim answer As VbMsgBoxResult
city = InputBox("Enter city name (e.g. Pune):", "FreshGo Report", "Pune")
If city = "" Then Exit Sub ' user pressed Cancel or left it empty
answer = MsgBox("Create report for " & city & "?", vbYesNo + vbQuestion, "Confirm")
If answer = vbYes Then
MsgBox "Report for " & city & " will be created.", vbInformation
Else
MsgBox "Cancelled.", vbExclamation
End If
End Sub
| Constant | Effect |
|---|---|
vbOKOnly, vbYesNo, vbYesNoCancel, vbOKCancel |
Buttons |
vbInformation, vbQuestion, vbExclamation, vbCritical |
Icon |
Return values vbYes, vbNo, vbCancel, vbOK |
Which button was clicked |
About this data: Teaching sample for a fictional shop “BrightLedger Finance”. Figures and names (e.g. Meera Joshi) are made up for this lesson on msgbox and inputbox.
For numbers or ranges use Application.InputBox: qty = Application.InputBox("Enter target orders", Type:=1) (Type 1 = number; Type 8 = a range the user selects).
Ravindra Bagale's Tip
If the user clicks Cancel in an InputBox, you get empty text, and many students' macros build a report for an empty city or throw an error. After an InputBox, always write a check like If city = "" Then Exit Sub. If you need a number, use Application.InputBox Type:=1 – then Excel does the validation itself.
Ravindra Bagale's Tip – मराठी
InputBox मध्ये user ने Cancel दाबलं तर रिकामा text येतो, आणि बऱ्याच students चा macro रिकाम्या city साठी report बनवतो किंवा error देतो. InputBox नंतर नेहमी If city = "" Then Exit Sub असा check लिहा. Number हवा असेल तर Application.InputBox Type:=1 वापरा – मग Excel स्वतः validation करतो.
Ravindra Bagale's Tip – हिंदी
InputBox में user ने Cancel दबाया तो खाली text आता है, और बहुत से students का macro खाली city के लिए report बना देता है या error देता है. InputBox के बाद हमेशा If city = "" Then Exit Sub जैसा check लिखो. Number चाहिए तो Application.InputBox Type:=1 इस्तेमाल करो – फिर Excel खुद validation करता है.
Worked example — msgbox and inputbox
Why
Rohan Patil in Pune and Liam Murphy abroad both need a clear, repeatable way to use msgbox and inputbox on a NovaCraft Tools sheet without breaking totals.
What
Msgbox And Inputbox is the Excel idea this page teaches. We will walk one tiny example end to end.
How
- Open a blank sheet. In A1 type a short label for msgbox and inputbox.
- Enter a small made-up value for Rohan Patil using ₹ where money appears.
- Add a second value for Liam Murphy using ₹ so you see mixed currencies stay numeric.
- Apply the method from this lesson step by step (menu or formula as shown above).
- Check the result still works inside SUM or a simple check cell.
Formula / action
Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Meaning: Once msgbox and inputbox is correct on one row, copy the same pattern down the column.
Practice tasks (10+)
Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.
1. Rebuild the smallest example of msgbox and inputbox on a blank sheet for Skyline Clinics.
Hint: Use only the steps on this page. Keep numbers numeric.
2. Change one input for Neha Shah and confirm the result updates.
Hint: If the result is stuck, check absolute vs relative references.
3. Repeat the same steps with a £ or € value for Anna Kowalski.
Hint: Formatting must not break SUM.
4. Write the result meaning in one plain sentence next to the cell.
Hint: Imagine explaining it to a new joiners.
5. Add a second row and fill the method down.
Hint: Double-click the fill handle if the pattern is consistent.
6. Break the example on purpose, then fix it using this lesson.
Hint: Common break: text instead of a number.
7. Apply msgbox and inputbox to a 5-row made-up Skyline Clinics table.
Hint: Keep column headers clear.
8. Check your answer with a simple total or COUNT.
Hint: A wrong total usually means a format or key mismatch.
9. Swap ₹ and $ on one column and re-check totals.
Hint: Stored values stay the same; only the display changes.
10. List two mistakes beginners make with msgbox and inputbox.
Hint: Look back at the tip box on this page.
11. Teach the steps out loud in under one minute.
Hint: Why → What → How.
12. Save the sheet as SkylineClin_practice.xlsx.
Hint: Use a clear file name for class.
Loading practice tasks...
- Orders on the sheet (Easy): How many orders are in the Order ID column?
- Late orders (Easy): How many rows have Status "Late"?
- Worst delay (Easy): What is the largest value in the Days late column?
- On-time orders (Easy): How many rows have Status "On time"?
- Late orders in the typed city (Medium): How many late orders are there in City "Pune"?
- Days late for an order (Medium): How many days late is Order ID "ORD-308"?
- Who is the customer? (Medium): Find the Customer for Order ID "ORD-304".
- Total days late (Medium): Add up the Days late column.
- Message text (Medium): Build the text "5 late orders" using the count of Late rows.
- Message for a city (Hard): Build the text "1 late orders in Pune".
- Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
- Warning or all clear (Hard): Return "Warning" if any order is 3 or more days late, else "All clear".
Quick revision cards
Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.


