VBA raises Runtime Error 5: Invalid procedure call or argument when a statement successfully invokes a built-in VBA function, system routine, or Excel method, but at least one argument passed to that procedure is outside its allowed range, negative when positive is required, or structurally invalid.
Quick Answer
Fix Runtime Error 5 by verifying that string manipulation functions (
Mid,Left,Right,InStr) do not receive zero or negative length/start arguments, that collection keys are valid strings, and that Excel method parameters conform to acceptable bounds.
Why Invalid Procedure Calls Happen
Runtime Error 5 indicates that the function exists and the data types match (ruling out Runtime Error 13: Type Mismatch (Trying to perform math on a string variable).), but the value of the argument makes the command impossible to execute.
Dim textVal As String
textVal = "Report"
' Mid expects start position >= 1. Passing 0 triggers Error 5:
Dim subVal As String
subVal = Mid(textVal, 0, 2)
Common Triggers and Fixes
1. String Functions with Out-of-Bounds Arguments
Built-in string functions (Mid, InStr, Left, Right) enforce strict parameter rules:
Mid(string, start, [length]):startmust be . Passing throws Error 5.InStr([start], string1, string2):startmust be .Left(string, length)/Right(string, length):lengthmust be . A negative length causes Error 5.
This frequently happens when a calculated position results in zero:
Dim rawString As String
Dim hyphenPos As Long
Dim cleanOutput As String
rawString = "ItemCode"
hyphenPos = InStr(rawString, "-") ' Returns 0 because "-" is not found
' Fails with Error 5 because hyphenPos - 1 equals -1:
' cleanOutput = Left(rawString, hyphenPos - 1)
' SAFE IMPLEMENTATION:
If hyphenPos > 1 Then
cleanOutput = Left(rawString, hyphenPos - 1)
Else
cleanOutput = rawString
End If
For corresponding worksheet formula errors with string lengths, see #VALUE! in LEFT/RIGHT/MID: Non-numeric length arguments.
2. Collection or Dictionary Calls with Invalid Keys
When reading from a Collection, passing an empty string or a non-existent index triggers this error in older libraries, or produces related runtime issues:
Dim coll As New Collection
coll.Add "Data", "Key1"
' If the key string is empty (""), VBA throws Runtime Error 5:
coll.Add "Data2", ""
To avoid key errors in dictionary and collection management, consult Runtime Error 457: This key is already associated with an element of this collection (Dictionaries)..
3. Operating System / Shell Calls
When using VBA’s Shell command or Windows API wrappers, passing an invalid window style or an improperly formatted executable command line triggers Runtime Error 5:
' Shell(pathname, [windowstyle])
' If windowstyle is out of the 0-6 range, Error 5 occurs:
Shell "notepad.exe", 99 ' Error 5: Invalid window style
Diagnostic Summary Table
| Error Code | Root Cause | Primary Fix |
|---|---|---|
| Runtime Error 11: Division by zero | Denominator evaluates to 0, empty cell, or uninitialized variable. | Wrap division in If divisor <> 0 Then; validate inputs. |
| Runtime Error 28: Out of stack space | Worksheet_Change event loops without stopping; recursive function lacks a base-case exit. | Set Application.EnableEvents = False; establish base-case exits. |
| Runtime Error 5: Invalid procedure call | Zero or negative index passed to string functions (Mid, Left); invalid parameter in system call. | Verify calculated string indices are ; validate arguments before function calls. |
Next Diagnostic Steps
If your runtime errors occur intermittently inside complex logic, learn to trap line numbers dynamically using How to find the exact line causing a crash using Erl..
To inspect variable values as they change before a calculation break or an event loop triggers, follow the real-time logging patterns in Debug.Print: The developer’s best friend for silent error logging. and variable monitoring via Using Watch Windows to track Object states..