Ravindra BagaleCourses & study guides मराठी Track your progress

12. Macros and VBA

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.

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

  1. Open a blank sheet. In A1 type a short label for msgbox and inputbox.
  2. Enter a small made-up value for Rohan Patil using ₹ where money appears.
  3. Add a second value for Liam Murphy using ₹ so you see mixed currencies stay numeric.
  4. Apply the method from this lesson step by step (menu or formula as shown above).
  5. 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).
Result: The demo cell shows the expected result for NovaCraft Tools's sample row.
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...

  1. Orders on the sheet (Easy): How many orders are in the Order ID column?
  2. Late orders (Easy): How many rows have Status "Late"?
  3. Worst delay (Easy): What is the largest value in the Days late column?
  4. On-time orders (Easy): How many rows have Status "On time"?
  5. Late orders in the typed city (Medium): How many late orders are there in City "Pune"?
  6. Days late for an order (Medium): How many days late is Order ID "ORD-308"?
  7. Who is the customer? (Medium): Find the Customer for Order ID "ORD-304".
  8. Total days late (Medium): Add up the Days late column.
  9. Message text (Medium): Build the text "5 late orders" using the count of Late rows.
  10. Message for a city (Hard): Build the text "1 late orders in Pune".
  11. Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
  12. 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.

Revision card 1: What is Msgbox And Inputbox?
Card 1: What is Msgbox And Inputbox? · Download PNG
Revision card 2: Tiny example — Msgbox And Inputbox
Card 2: Tiny example — Msgbox And Inputbox · Download PNG
Revision card 3: Checklist — Msgbox And Inputbox
Card 3: Checklist — Msgbox And Inputbox · Download PNG