Fix VBA Optional Argument or Wrong Number of Arguments Error
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.

- 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 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.
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.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and locate the module containing your code.
Find the line of code where the 'insertarregistro' function is called. It will likely look like a list of seven variables separated by commas.
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.
Click 'Debug' > 'Compile VBAProject' in the top menu to ensure no further errors exist, then run your macro.

Modify the Function Declaration to Match the Call
If the missing argument is no longer necessary for your macro to function, remove it from the function declaration to match the seven arguments you are currently passing.
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. Download WPS Office: Download and install WPS Office from the official website.
- 2. Open Your Spreadsheet: Open your macro-enabled spreadsheet (.xlsm) in WPS Spreadsheet.
- 3. Access the VBA Editor: Navigate to the 'Developer' tab and click 'Visual Basic' to access the built-in VBA Editor.
- 4. Edit and Debug: Edit your function calls and declarations with full syntax highlighting and debugging tools to quickly resolve argument errors.

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.




