Runtime Error 5: “Invalid procedure call or argument,” explained and fixed

Error 5 — “Invalid procedure call or argument” — means the code called a function with a value that function cannot accept: the square root of a negative number, a string operation starting before the string does, a file path the Shell can't parse. The call was well-formed; one of its inputs was impossible.

The usual impossible inputs

  1. String positions out of range — Mid(text, start) with start ≤ 0, or Left/Right with a negative length, often because a Len() or InStr() returned 0 for text that didn't contain what was expected.
  2. Math that has no answer — Sqr(-1), Log of zero or a negative, division results fed onward unchecked.
  3. A search that found nothing — InStr returns 0 when the substring isn't there; arithmetic built on 0 produces invalid positions downstream. This is the leading cause in string-parsing macros.
  4. File and Shell arguments — a path with bad characters, or an empty string where a file name belongs.
  5. Dates out of range — DateSerial or date arithmetic pushed outside VBA's valid date span by bad input.

The fix

  1. Click Debug and read the highlighted line's inputs. Hover each argument; compute what each function on that line is actually receiving. The impossible one is visible immediately.
  2. Guard searches:
    pos = InStr(fullText, ",")
    If pos > 0 Then surname = Left(fullText, pos - 1)
    Never let an InStr result flow into Mid/Left unchecked.
  3. Validate at the boundary: check Len(), sign, and range on values coming from cells, files, or users before the call — If x >= 0 Then root = Sqr(x).
  4. Test data first: error 5 that appears “sometimes” is data-dependent — a name without a comma, an empty field, a negative after a refund. Find the triggering record by logging inputs before the call (Debug.Print).
Do not download anything for this error. It's an argument inside the program's own logic. The fix is one validation check in the right place.

If it still happens

If the line looks innocent, the bad value arrived from further away — a function upstream returned 0, -1, or an empty string and nobody checked. Put a breakpoint where the value is created and watch it flow forward. In legacy compiled apps hit by error 5, the equivalent story is bad input data or settings; a clean settings reset or the specific problem record is the fix, and the vendor can use the exact steps that trigger it.

Common questions

What's the difference between error 5 and error 13? 13 is the wrong kind of data (text as a number). 5 is the right kind of data with an impossible value (a number, but negative where negatives can't work).

Why does it only fail on some records? Because only some records produce the impossible argument — the entry without the separator, the refund that makes a total negative, the empty field. Data-dependent errors are input validation gaps.

Related errors

Runtime Error 11 · Runtime Error 13 · Runtime Error 1004