VBA CSng Function in Excel is categorizes as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CSng function converts an expression to a Single data type.
This function use in either procedure or function in a VBA editor window in Excel. We can use this VBA CSng Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CSng function, where we can use this VBA CSng Function and it’s real-time examples.
- Overview
- Syntax of VBA CSng Function
- Parameters or Arguments
- Where we can apply or use VBA CSng Function?
- Example 1: Convert a String(100) to Single Data Type
- Example 2: Convert a Value(12345.678) to Single Data Type
- Example 3: Convert a Value(4.402823E+38) to Single Data Type (Returns an Overflow Error)
- Example 4: Convert a string(Test) to Single Data Type (Returns an Type mismatch Error)
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA CSng Function:
The syntax of the CSng Function in VBA is
CSng(Expression)
The CSng function returns a single data type value.
Parameters or Arguments:
The CSng function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a value. It is used to convert the value to a single value.
Where we can apply or use VBA CSng Function?
We can use this CSng Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Convert a String(100) to Single Data Type
Here is a simple example of the VBA CSng function. This below example specified an expression(100) converts to a single type value.
'Convert a String(100) to Single Data Type Sub VBA_CSng_Function_Ex1() 'Variable declaration Dim sValue As String Dim dResult As Single sValue = 100 dResult = CSng(sValue) MsgBox "String(100) to Single Data Type : " & dResult, vbInformation, "VBA CSng Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Convert a Value(12345.678) to Single Data Type
Here is a simple example of the VBA CSng function. This below example specified an expression(12345.678) converts to a single type value.
'Convert a Value(12345.678) to Single Data Type Sub VBA_CSng_Function_Ex2() 'Variable declaration Dim sValue As Double Dim dResult As Single sValue = 12345.678 dResult = CSng(sValue) MsgBox "Value(12345.678) to Single Data Type : " & dResult, vbInformation, "VBA CSng Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Convert a Value(4.402823E+38) to Single Data Type (Returns an Overflow Error)
Here is a simple example of the VBA CSng function. This below example specified an expression(4.402823E+38) converts to a single type value. It returns an Overflow error. The value 4.402823E+38 is greater than 3.402823E38. The long value should be -3.402823E38 to -1.401298E-45 for negative values and 1.401298E-45 to 3.402823E38 for positive values.
'Convert a Value(4.402823E+38) to Single Data Type (Returns an Overflow Error) Sub VBA_CSng_Function_Ex3() 'Variable declaration Dim sValue Dim dResult As Single sValue = 4.402823E+38 dResult = CSng(sValue) MsgBox "Value(4.402823E+38) to Single Data Type : " & dResult, vbInformation, "VBA CSng Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Convert a string(Test) to Single Data Type (Returns an Type mismatch Error)
Here is a simple example of the VBA CSng function. This below example specified expression(Test) converts to a long type value. It returns Type mismatch error.
'Convert a string(Test) to Single Data Type (Returns an Type mismatch Error) Sub VBA_CSng_Function_Ex4() 'Variable declaration Dim sValue As String Dim dResult As Single sValue = "Test" dResult = CSng(sValue) MsgBox "String(Test) to Single Data Type : " & dResult, vbInformation, "VBA CSng Function" End Sub
Output: Here is the screen shot of the fourth example output.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers