alt= compilation error: not optional argument data-orig-width=385 data-orig-height=274 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2020/12/How-Do-You-Fix-Compile-Error-Argument-not-optional.png data-ez= />
alt=Plug in! data-ezsrc=/utilcave_com/social/pin_it.png />
alt=Change to Facebook data-ezsrc=/utilcave_com/social/fb_share.png />
Before looking at the solution to the bug described above, we will go back to basics to make sure you understand what a feature is. This will be important to correct and solve the problem. If you’ve already understood the concept, you can skip it.
What is a function?
A code block that can be used several times can call up a function. The word reusable tells us that the function can be called up an unlimited number of times from anywhere in the project.
If we need to expand or modify an item already included in a function, we just do it in one place – in a function. It will process changes in the current where the function has been called.
A block of code with the name of a function that is called in different places is called the calling function, and the code line that calls the function is called the calling function.
Arguments in function
Suppose you have the necessary data to execute a block of function code by programming. This data can be entered in the same line as the name of the function.
These data are referred to as arguments or parameters in the programming. When you define a function, you can specify the type of these arguments and the names of the variables. However, this is not mandatory.
Based on the use of variable arguments in the code, we can also assume the type of these arguments.
Value returned in function
After starting the code block, information may appear indicating that the function wants to return to the call function. This is called a return value. Optionally, you can also specify the data type of the returned value when defining the function.
Determining a function with arguments and the return value
Function
Let’s imagine a position called up by the subprocedure to save the button on the student’s admission form. Let’s call the fn_SaveStudent function. The function takes all the values of the form fields from the GUI and inserts them into the database as a student record.
Arguments
Field values must be passed as arguments from the calling function to the called function.
Eligible value
The database in turn automatically generates the student card. The function can return this value to the call function, which in turn can display the student number in each field of the Application Form or in the message field.
Syntax of the function
Function < function name> ([arg1 [, arg2 [, arg3 [, …… ]).
Last Function
If the argument is an argument.
For example:
Sub – Procedure when clicking the save button
Private Sub cmdSave_Click()
strName = txtName.value
intAge = lstAge.value
strGen = lstgender.value
lngContact = txtcontact.Value
Call of the function fn_SaveStudent (strName, intAge, strGen, lngContact)
End Sub
fn_SaveStudent (strName As String, intAge As Integer, strGen As String, lngContact As Long) As String
‘ code to insert variable values into the database parameters and return the generated End Student Id
fnSaveStudent = strstudentId
End Function.
Integrated functions
These functions are predefined blocks of code provided by programming languages for different purposes. With VBA we have a wide range of integrated functions. Some examples are given below.
no | Name of the function | Brief description | Syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1 | Section | Cut a line with a character as a separator. A series of words will be returned. | PARTS [,] [,] [,] [,] [,] [,] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 | Cdbl | Conversion of an expression to a duplicate of a data type | CDBL ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3 | chr | Returns a string (character value) corresponding to the value transmitted as a parameter. | Oh, my God. Oh, my God. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4 | Environment | Returns the middle part of the line if the start position and length are given. | LLIN (, [, ]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5 | On the left. | Returns the left side of the line if the length is specified. | LEFT… | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
6 | It’s true. | Returns the right side of the line, if the length is specified. | RULE ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
7 | Cutting | Cut the conductor and the final distance between the wires. | Cutting ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8 | Ucase | Converts all characters of a string to uppercase letters | Ucase ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
9 | Corpus | Converts all characters from a string to lowercase letters | Annex ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
10 | Tools | Checks whether one line is an underlayer of another. If yes, the starting position will be returned, otherwise 0 will be returned. | INSTR([] , , , [,] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
11 | Right now. | Returns the date and time of the current system | now() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
12 | Cstr | Converts the value of the transmitted parameter into a string | cstr ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
13 | Val | Returns the mathematical value of the parameter. | Value ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
14 | Ltrim | Removes leading spaces from the string and returns them. | Ltreme | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15 | Rtrim | Removes the spaces at the end of the string and returns it. | Search | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
16 | Date | Returns the date of the current system | Date ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
17 | Hello | Returns the day of the transferred date setting. | Day () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
18 | Péché | Returns the value of sin | Sin ( <expression>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
19 | Tanning | Returns the value of the tangent | Tan ( < expression>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
20 | Chat | Returns the value of Kotangent | Cat ( < expression> ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
21 | Magazine | Returns the logbook value | Magazine ( <expression>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
22 | Because, uh… | Returning because | Because ( < expression>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
23 | Isnull | Returns the boolean value depending on whether the parameter value is zero or not. | Grown up. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
24 | Isnumeric | Returns the boolean value depending on whether the parameter value is a numerical value or not. | Isolated ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
25 | Rate | Returns a duplicate of the value of the data type as an interest rate for a series of equal cash flows at regular intervals. | COMMAND ( , , , , , [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
26 | Cint | Conversion of a parameter value to an integer value | scint ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
27 | Mkdyr | Create a folder, i.e. create a folder. | Macdir | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
28 | Cvar | Conversion of the parameter value to the value of the data type of the option | RBAF (expression) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
29 | Ccur | Converts the parameter value to the value of the currency type. | Corn ( ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
30 | Date | Converts the parameter value to the date | Date ( ) |
Optional Arguments and Errors Non-optimal Argument
User-defined functions
For user-defined functions, arguments are considered optional or mandatory depending on whether the OPTIONAL keyword was used to define the parameters of the called function.
Example
In this retrievable function, no argument with an optional keyword is defined. So if the argument is not passed to the calling function, we get the error message Argument is not optional.
alt= Optional non-compile the error argument data-orig-width=1017 data-orig-height=441 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2020/12/1607260082_453_How-Do-You-Fix-Compile-Error-Argument-not-optional.png data-ez= />
alt=Plug in! data-ezsrc=/utilcave_com/social/pin_it.png />
alt=Change to Facebook data-ezsrc=/utilcave_com/social/fb_share.png />
To solve this problem, we need to pass the parameter to the calling function or mark the rollno argument as optional. The following code fragment corrects the error.
Sub demo1()
Call fn_demo1(Baby Aneesh, A+)
End Sub
Function fn_demo1(strname, strgrade, Optional ByVal introllno as Integer)
‘Just show the values of all
parameters MsgBox student name : Name and vbCrLf and class: and strgrade & vbCrLf & roll no.: and intro
Final function
Integrated functions
For built-in functions, the syntax clearly indicates whether the parameters are optional or mandatory.
Example
Syntax of the distribution function of the above table :
PARTS [,] [,] [,] [,] [,] [,]
Here is only an expression, i.e. a string, a mandatory argument. This means that the code works successfully even if the last two optional arguments are not passed.
alt=function that works data-orig-width=960 data-orig-height=540 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2020/12/1607260083_377_How-Do-You-Fix-Compile-Error-Argument-not-optional.png data-ez= />
alt=Plug in! data-ezsrc=/utilcave_com/social/pin_it.png />
alt=Change to Facebook data-ezsrc=/utilcave_com/social/fb_share.png />
However, if the mandatory argument is not passed, this will lead to a compilation error. The argument is not optional.
alt= Optional non-compile the error argument data-orig-width=655 data-orig-height=437 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2020/12/1607260083_707_How-Do-You-Fix-Compile-Error-Argument-not-optional.png data-ez= />
alt=Plug in! data-ezsrc=/utilcave_com/social/pin_it.png />
alt=Change to Facebook data-ezsrc=/utilcave_com/social/fb_share.png />
To correct this compilation error, you must provide mandatory arguments when calling the function.
Conclusion
The compilation error discussed in this article is obvious if you understand the purpose and use of the functions in general. We must provide all mandatory arguments of the expected data type to avoid such compilation errors.
Related Tags:
word vba find argument not optional,argument not optional javascript,excel vba sub with optional arguments,vba byref,argument not optional vba range,vba argument not optional when calling sub,compile error: argument not optional when calling sub,vba argument not optional when calling function,basic runtime error: argument is not optional,internet explorer argument not optional,parameter not optional exception from hresult: 0x8002000f (disp_e_paramnotoptional),javascript runtime error: argument not optional,argument not optional excel