30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA Invalid Procedure Call Or Argument Error (Error 5)
By Puneet Gogia (Microsoft MVP)

VBA Invalid Procedure Call Or Argument Error (Error 5)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Error 5, “Invalid procedure call or argument”, means a VBA function got a value it can’t work with. Common examples are Mid(text, 0, 3), where the start must be 1 or more; Left(text, -1), a negative length; Sqr(-4), a negative number; Log(0); and Chr(300), where codes run from 0 to 255.

The bad value is usually calculated, so the error shows up only with certain data. Left(fullName, InStr(fullName, " ") - 1) works for “Asha Rao” but fails for a name with no space, because InStr returns 0 and the length becomes -1. Check first with If InStr(fullName, " ") > 0 Then.

Click Debug on the error message to highlight the line, then hover over each variable, or type ? and the variable name in the Immediate window (Ctrl + G), to see which value is out of range.

Error 5 comes from VBA’s own functions. Excel’s objects report a bad argument as error 1004 instead, as with Range("A0"), so look up 1004 when the failing line uses a range, sheet or workbook.

As Microsoft says, an Invalid Procedure Call Or Argument (Error 5) occurs when you have specified a value that is out of the range of the argument, or you are using a procedure that is not available on the current Excel platform. So, the error comes from one of these two problems, and each of them can have different reasons.

For example, you are using the VBA function SIN in the code. Now, while specifying the value for the argument, if you go out of the range of numbers it accepts, VBA generates this error.

Or let’s say you have written a code in the Windows version of Excel, and when you try to run the same code in the Macintosh version, you get Error 5 Invalid Procedure Call or Argument because the procedure you are trying to run is not valid there.

Common Examples of Error 5 in VBA

In real codes, you mostly get this error when a value that you pass to a built-in function is not allowed. Here are the ones you’ll come across the most:

  • Text functions with a zero or negative number: Left, Right and Mid need a length of 0 or more, and Mid and InStr need a start position of 1 or more. So Left(“Excel”, -1) and Mid(“Excel”, 0, 2) both give error 5.
  • Square root or log of a number that’s not allowed: Sqr needs a number that’s 0 or more, and Log needs a number greater than 0. So Sqr(-4) and Log(0) give error 5.
  • Space and String with a negative number: Space(-1) or String(-1, “*”) can’t create a negative number of characters.
  • Windows-only features on a Mac: Things like ActiveX controls, Windows API calls (Declare statements) and some file and folder functions work only on Windows.

Most of the time, the negative number comes from a calculation in your code. In the code below, InStr returns 0 when the cell has no space, so the length for Left becomes -1 and VBA shows the error:

Sub GetFirstName()
Dim fullName As String
fullName = Range("A1").Value
MsgBox Left(fullName, InStr(fullName, " ") - 1)
End Sub

How to Fix Error 5 (Invalid Procedure Call or Argument)

First of all, click the Debug button on the error message, and VBA highlights the line with the problem. After that, check the values that you pass to the function on that line. You can hover over a variable to see its value, or type a question mark and the expression in the Immediate Window (Ctrl + G), like ?InStr(fullName, ” “), and press Enter.

Once you know which value is wrong, test it before you call the function. Here’s the same code fixed, where it uses the whole text when there’s no space:

Sub GetFirstName()
Dim fullName As String
Dim spacePos As Long
fullName = Range("A1").Value
spacePos = InStr(fullName, " ")
If spacePos > 0 Then
MsgBox Left(fullName, spacePos - 1)
Else
MsgBox fullName
End If
End Sub

In the same way, check that a number is 0 or more before Sqr and greater than 0 before Log. And for code that has to run on both Windows and Mac, you can test the platform with conditional compilation (#If Mac Then … #Else … #End If) and run the right code for each.

Frequently Asked Questions

What Is the Difference Between Error 5 and Error 13?

Error 13 (Type mismatch) means the value is the wrong type, like text where a number is needed. Error 5 means the type is right but the value isn’t allowed, like a negative number for a length.

Why Does My Code Give Error 5 Only for Some Cells?

The value in those cells leads to an argument that isn’t allowed, like an empty cell, a text without the character you search for, or a negative number. Check the value of the variables on the highlighted line when the error shows up.

Can I Use On Error to Handle Error 5?

Yes. In an error handler, you can check if Err.Number is 5 and show a message or use a default value. But it’s better to test the value before calling the function, so you know exactly which case you are handling.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel