VBA & Macro Problems

Fix VBA Optional Argument or Wrong Number of Arguments Error

Olivia MillerOlivia Miller Oct 22, 2026 872 views

Question details

The user is encountering a VBA compilation error because a function call is missing a required argument that was specified in the function declaration.

Fix VBA Optional Argument or Wrong Number of Arguments Error
Product
Spreadsheet
Device & OS
not provided
Scenario
Executing a VBA macro that calls a custom function to insert records into a database, but failing to pass all the required parameters.
Observed behavior
A compilation error occurs stating "Argument not optional" or "Wrong number of arguments" because the function requires eight arguments, but only seven are supplied.
Before you start

Before modifying your VBA code, ensure you understand the original purpose of the missing parameter and save a backup copy of your macro-enabled workbook to prevent accidental code loss.

Solution 1Recommended

Supply the Missing Argument in the Function Call

Update the procedure call to include all required arguments in the exact order specified by the function declaration.

When a VBA function is declared with a specific number of required arguments, failing to provide all of them triggers a compile error. In this scenario, the 'insertarregistro' function expects eight arguments, but the call only supplies seven.

You must identify the missing argument (in this case, 'placa') and insert its corresponding value into the correct position within the calling statement.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and locate the module containing your code.

2
Locate the Function Call

Find the line of code where the 'insertarregistro' function is called. It will likely look like a list of seven variables separated by commas.

3
Insert the Missing Variable

Add the missing 'txt_placa' variable in the correct sequence. The updated call should look like: insertarregistro txt_id, txt_placa, txt_equipo, txt_marca, txt_serie, txt_motor, txt_ano, txt_estatus.

4
Compile and Run

Click 'Debug' > 'Compile VBAProject' in the top menu to ensure no further errors exist, then run your macro.

Supply the Missing Argument in the Function Call
Match Data Types: Ensure that the variable you add matches the data type defined in the function declaration, such as passing a String where a String is expected.
Advanced VBA Support

Seamlessly Write and Debug VBA Macros with WPS Office

WPS Office provides robust support for VBA and Macros, allowing you to seamlessly run, edit, and debug your automated workflows just as you would in Microsoft Excel.

  1. 1. Download WPS Office: Download and install WPS Office from the official website.
  2. 2. Open Your Spreadsheet: Open your macro-enabled spreadsheet (.xlsm) in WPS Spreadsheet.
  3. 3. Access the VBA Editor: Navigate to the 'Developer' tab and click 'Visual Basic' to access the built-in VBA Editor.
  4. 4. Edit and Debug: Edit your function calls and declarations with full syntax highlighting and debugging tools to quickly resolve argument errors.
Fully compatible with Microsoft Excel VBA code and macro formats (.xlsm, .xlsb).Built-in VBA editor for seamless code management and debugging.Lightweight architecture for fast execution of heavy data processing macros.Free and comprehensive alternative for everyday professional tasks.
microsoft office alternative - wps office

Frequently Asked Questions

What does 'Argument not optional' mean in VBA?

This compile error occurs when you call a function or subroutine but fail to provide one or more arguments that the procedure strictly requires to execute.

How can I make a VBA function argument optional?

You can use the 'Optional' keyword in the function declaration directly before the argument name. For example: Function MyMacro(arg1 As String, Optional arg2 As String).

Why does my macro work on one PC but give an argument error on another?

This typically happens if different versions of a custom library or add-in are installed, or if the macro calls an external function whose signature has been updated. You can check your library references in the VBA Editor by going to Tools > References.

How do I step through my VBA code to find the exact error?

Open the VBA Editor, place your cursor inside the macro, and press F8 to execute the code line by line. This debugging method helps you identify exactly which function call is triggering the error due to missing arguments.