Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type).

Syntax

CSng (Expression)

Parameter

Expression (required) – This parameter represents the expression that you want to convert to a single data type.

Return

This function returns a single-precision floating-point number after converting the supplied expression into a single data type.

Example 1

Sub CSngFunction_Example1()
 ' Converting the string and numeric value into Single
 Dim sng As Single
 sng = CSng("0.99999")
 ' The variable sng will return 0.999989986
 Cells(1, 1).Value = sng
 End Sub 

Output

0.999989986

VBA CSng Function

Example 2

Sub CSngFunction_Example2()
 ' Converting the string and numeric value into Single
 Dim sng As Single
 sng = CSng(2.01 * 2.01)
 ' The variable sng will return 4.040100098
 Cells(1, 1).Value = sng
 End Sub 

Output

4.040100098

VBA CSng Function

Example 3

Sub CSngFunction_Example3()
 ' Converting the string and numeric value into Single
 Dim sng As Single
 sng = CSng("Hello VBA")
 ' The variable sng will return a run-time error
 Cells(1, 1).Value = sng
 End Sub 

Output

VBA CSng Function