Excel VBA conversion functions – How to convert variable

Last Updated on January 11, 2023 by token

VBA conversion functions – Description. How to change data type in VBA Excel?

In this chapter, you will learn all convertion functions in Excel VBA . When writing programs in VBA, you will have often a situation where you will have to change the data type. As you probably know, the date itself can be written in several different ways in VBA Excel. You can also see a situation where numbers are stored as text. To use them and perform arithmetic operations, you should change VBA data types for them. This is exactly what you will learn about VBA conversion functions from this article.

List of conversion functions in VBA.

The table lists all data type conversion functions in VBA Excel.

FUNCTION DESCRIPTION
CBOOL () The function returns the value of the argument as a Boolean data type. Stores True or Flase values.
CBYTE () The function returns the value of the argument as a Byte data type.
CCUR () The function returns the value of the argument as a Currency data type.
CDATE () The function returns the value of the argument as a Date data type.
CDBL () The function returns the value of the argument as the Double data type.
CDEC () The function returns the value of the argument as a Decimal data type. The Decimal data type no longer works in Excel 2016. The function return can be compared to the Double type.
CINT () The function returns the value of the argument as a Double data type.
CLNG () The function returns the value of the argument as a Long data type.
CLNGLNG () The function returns the value of the argument as a LongLong data type. It only works on 64bit architecture.
CLNGPTR () The function returns the value of the argument as a LongPtr data type. It works in the 32bit and 64bit architecture.
CSNG () The function returns the value of the argument as a the Single data type.
CSTR () The function returns the value of the argument as a String data type.
CVAR () The function returns the value of the argument as a Variant data type.

Examples of using the conversion function.

Below is an example of using VBA conversion functions in Excel.

Sub ConversionFunctionsVba ()

Dim lngExample As Long
 lngExample  = 123.12
Dim strExample As String
 strExample  = "2020-01-01"
Dim intExample As Integer
 intExample  = 123

Range ("A2") = CBool ​​(1 = 2)
Range ("A3") = CByte (LngVariable)
Range ("A4") = CCur (12345.6789)
Range ("A5") = CDate (Variable StrV)
Range ("A6") = CDbl (LngVariable)
Range ("A7") = CDec (LngVariable)
Range ("A8") = CInt (LngVariable)
Range ("A9") = CLng (intVariable)
Range ("A10") = CLngLng (LngVariable)
Range ("A11") = CLngPtr (LngVariable)
Range ("A12") = CSng (intVariable)
Range ("A13") = CStr (LngVariable)
Range ("A14") = CVar (LngVariable)

End Sub
VBA Tutorial - VBA Conversion Functions
VBA Tutorial – VBA Conversion Functions

I attach the file with examples below:

Tasks (you can enter the solution in the comment)

1. Enter the numeric variable with the value 123456789.987654321 into VBA. Check the variable for converting to all text and numeric data types. Display the result as one MsgBox VBA window .

Source: VBA Functions

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: