×

Excel VBA Cos Function

VBA Cos Function: The Cos function in VBA returns the cosine value for a supplied angle.

Syntax

Cos (Number)

Parameter

Number (required) –This parameter represents the number that you want the absolute value of.

Return

This function returns the cosine value for a supplied angle.

Example 1

Sub CosFunction_Example1()
 ' Calculating the cosine for the specified number.
 Dim cos_val1 As Double
 Dim cos_val2 As Double
 Dim cos_val3 As Double
 cos_val1 = Cos(90)
 ' The variable cos_val1 will return value -0.448073616.
 Cells(1, 1).Value = cos_val1 
 cos_val2 = Cos(0)
 ' The variable cos_val2 will return value 1.
 Cells(2, 1).Value = cos_val2
 cos_val3 = Cos(3.14159265358979)
 ' The variable cos_val3 will return value -1.
 Cells(3, 1).Value = cos_val3
 End Sub 

Output

-0.448073616
1
-1
 
VBA Cos Function

Example 2

Sub CosFunction_Example2()
 ' Calculating the cosine for the specified number.
 Dim cos_val As Double
 cos_val1 = Cos(0.785398163397448)
 ' The variable cos_val1 will return value 0.
 Cells(1, 1).Value = cos_val
 End Sub 

Output

0

VBA Cos Function

Example 3

Sub CosFunction_Example3()
 ' Calculating the cosine for the specified number.
 Dim cos_val As Double
 Const pi = 3.14159265358979
 ' Converting 60 degrees to radians by multiplying by pi/180.
 cos_val = Cos(60 * pi / 180)
 ' The variable cos_val will return radina 0.500000000000001.
 Cells(1, 1).Value = cos_val
 End Sub 

Output

VBA Cos Function

Example 4

Sub CosFunction_Example4()
 ' Calculating the cosine for the specified number.
 Dim cos_val As Double
 'for other formats apart from number
 cos_val = Cos("42/a")
 ' The variable atn_val1 will return mismatch error.
 Cells(1, 1).Value = cos_val
 End Sub 

Output

VBA Cos Function

Related Topics

Excel VBA UBound Function

Excel VBA UBound Function: The UBound function in VBA returns the highest subscript for the specified dimension in the given array. Syntax UBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

VBA Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.

VBA Regex

What is a Regex? Regex stands for Regular Expression is basically a pattern matching strings within another string. They are supported in many languages, including .net, C++, Python, etc. They are...

5 minutes read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

VBA UBound

VBA UBound  The UBound or Upper Bound function in VBA is used to specify the length of an array and returns the highest subscript for a dimension for the specified array. It is...

3 minutes read.

Excel VBA Str Function

VBA Str Function: The Str function in VBA converts the given number into a string representation of that number. Syntax Str (Number) Parameter Number (required) – This parameter represents the numeric value that you want...

1 minute read.

Excel VBA IsObject Function

VBA IsObject Function: The IsObject function in VBA returns a Boolean value showing whether the specified variable represents an Object variable type or not. Syntax IsObject (Expression) Parameter Expression (required)- This parameter represents the...

1 minute read.

Excel VBA While wend Loop

WHILE wend loop is used when the user is not sure how many times they want to execute the VBA code within the program. With a WHILE loop, the loop body may...

3 minutes read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA StrConv Function

The StrConv function in VBA converts a string into a specified format. Syntax StrConv (String, Conversion, [LocaleID]) Parameter String (required) – This parameter represents the string to be converted. Conversion (required) – This parameter specifies the type of conversion. It can...

1 minute read.

Excel VBA IsNumeric Function

VBA IsNumeric Function: The IsNumeric function in VBA returns a Boolean value showing whether the specified Expression contains a numeric value or not. Syntax IsNumeric (Expression) Parameter Expression (required)- This parameter represents the variant that...

1 minute read.

Excel VBA CVar Function

VBA CVar Function: The CVar function in VBA converts an expression into a Variant data type. Syntax CVar (Expression) Parameter Expression (required) – This parameter represents the expression that that you want to convert...

1 minute read.

Excel VBA InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

VBA Dim

What is Dim? DIM or Dimension or Declare in Memory is a keyword that is used in VBA to declare a variable with the different data types (Integer, String, variable, Boolean, Double, etc.)...

7 minutes read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.

Excel VBA FormatDateTime Function

VBA FormatDateTime Function: The FormatDateTime function in VBA returns the result as a string after applying a date and/or time format to the supplied expression. Syntax FormatDateTime (Expression, [NamedFormat]) Parameter Expression (specified) – This parameter...

2 minutes read.