Wauvel

Excel like a finance pro.

← All VBA snippets

Ask a question before the macro runs

A confirmation and an input box — the difference between a macro only you dare run and one the team can use.

1What it does

A macro that deletes rows the instant it's clicked is a macro nobody else will touch. `MsgBox` with `vbYesNo` asks first; `Application.InputBox` collects a number, or lets someone select a range with the mouse, and both give a Cancel that leaves the sheet untouched. Cancel is the part worth getting right — it reports itself differently depending on what you asked for, and the obvious check for it has a trap that makes zero look like a cancellation.

2The code

Sub DeleteBelowThreshold()
    ' Type:=1 asks for a number. Cancel returns the Boolean False.
    Dim threshold As Variant
    threshold = Application.InputBox( _
        Prompt:="Delete rows with an amount BELOW:", _
        Title:="Threshold", Default:=1000, Type:=1)

    ' Check the TYPE, not the value: a user typing 0 also equals False.
    If VarType(threshold) = vbBoolean Then Exit Sub

    ' Type:=8 lets them select a range with the mouse. Cancel raises an
    ' error here rather than returning False, so it needs the other guard.
    Dim rng As Range
    On Error Resume Next
    Set rng = Application.InputBox("Select the amount column", "Range", Type:=8)
    On Error GoTo 0
    If rng Is Nothing Then Exit Sub

    Dim answer As VbMsgBoxResult
    answer = MsgBox("Delete every row under " & Format(threshold, "$#,##0") & _
                    " in " & rng.Address(False, False) & "?" & vbNewLine & _
                    "This can't be undone.", _
                    vbYesNo + vbExclamation, "Confirm")
    If answer <> vbYes Then Exit Sub

    ' Bottom-up, so deleting a row doesn't skip the next one.
    Dim r As Long
    For r = rng.Rows.Count To 1 Step -1
        If rng.Cells(r, 1).Value < threshold Then rng.Rows(r).EntireRow.Delete
    Next r
End Sub

Paste into the Visual Basic Editor (Alt + F11 → Insert → Module), then run and save as macro-enabled (.xlsm).

New to macros? Set up in 5 minutes
  1. 1

    Don't see the Developer tab in the ribbon?

    You don't strictly need it — Alt + F11 opens the editor directly — but it makes running macros easier.
    • Windows: File → Options → Customize Ribbon → tick Developer in the right-hand list → OK.
    • Mac: Excel → Preferences → Ribbon & Toolbar → tick Developer → Save.
  2. 2

    Paste in the code

    Press Alt + F11 to open the Visual Basic editor, then Insert → Module and paste the snippet's code into the blank window. Close it with Alt + Q.
  3. 3

    Run it

    Press Alt + F8, pick the macro's name, and click Run — that's it. (Pasted a custom function instead? Just type it into a cell like any built-in: =GrossMargin(B2, B3).)
  4. 4

    Keep the macro — save as .xlsm

    File → Save As → Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx silently drops the code when you save.
  5. 5

    Macros blocked?

    Click Enable Content on the yellow bar. If you downloaded the file, you may first need to right-click it → Properties → tick Unblock → OK, then reopen.

Heads up: macros can't be undone with Ctrl + Z — save a copy before running one that changes your workbook.

3When you use it

  • Confirm before anything that deletes, overwrites or sends.
  • Collect a cut-off date, a threshold or a month without hard-coding it.
  • Let someone point at the range instead of describing it in a prompt.

4See it in action

A simulation — press Run to perform what the macro does to a sample workbook.

2INV-1041$12,400
3INV-1042$640
4INV-1043$8,900
5INV-1044$180
6INV-1045$44,100

5Pitfalls

`If threshold = False Then Exit Sub` treats a typed 0 as a cancellation, because VBA compares False to 0 as equal.

Fix: Test `VarType(threshold) = vbBoolean`, which is only true when the user actually cancelled.

Cancel on a range prompt (`Type:=8`) raises a run-time error rather than returning False.

Fix: Wrap that one line in `On Error Resume Next` and check `If rng Is Nothing`.

`InputBox` alone — VBA's own function rather than `Application.InputBox` — can't take a range and returns everything as text.

Fix: Use `Application.InputBox` and its `Type` argument whenever you need a number or a range.

The confirmation says what the macro is called rather than what it will do.

Fix: Put the actual count and range in the prompt, as above. People read the number, not the title.

6No-code alternatives

  • A cell on a settings sheet Better for something that rarely changes — visible, editable, and it doesn't ask every single run.
  • A UserForm Right when there are several inputs at once, at the cost of a designer to maintain.

Rather not write macros? Wauvel's free tools generate branded, formula-driven Excel for you — no VBA required.

Learn the moves here — or let Wauvel run them on your numbers.

Meet your AI CFO →

One CFO-grade Excel tip a week

A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.