Office tutorials

Excel VBA conversion functions – How to convert variable

VBA conversion functions – Description. How do you change the data type in VBA Excel?

In this chapter you will learn all the conversion functions in Excel VBA . When writing programs in VBA, you will often encounter a situation in which you will have to change the format of the data. The direct cause here can be the discrepancy between the methods of data recording. As you probably know, the date itself can be saved in several different ways. You can also find a situation in which numbers are stored as text. To use them and perform arithmetic operations, you should change the VBA data types for them . For this purpose, you will learn the functions of the VBA conversion .

The list of conversion functions in VBA.

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

 
FUNCTION DESCRIPTION
CBool ​​() The function returns the value of the argument with the Boolean data type. Stores True or Flase values.
CByte () The function returns the value of the argument with the Byte data type.
CCUR () The function returns the value of the argument with the Currency data type.
CDATA () The function returns the value of the argument with the Date data type.
CDbl () The function returns the value of the argument with the Double data type.
CDEC () The function returns the value of the argument with the 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 with the Double data type.
CLng () The function returns the value of the argument with the Long data type.
CLNGLNG () The function returns the value of the argument with the LongLong data type. It works only in 64bit architecture.
CLNGPTR () The function returns the value of the argument with the LongPtr data type. It works in 32bit and 64bit architecture.
CSng () The function returns the value of the argument with the Single data type.
CSTR () The function returns the value of the argument with the data type String.
CVAR () The function returns the value of the argument with the Variant data type.

An example using the conversion function.

Below is an example using the Excel VBA function and conversion.

I attach the file with examples below:

xlsm

Tasks (You can enter the solution in a comment)

  1. Enter a numeric variable of 123456789.987654321 into VBA. Check the variable for converting to all types of text and numeric data. Display result in the form of one MsgBox VBA window .

Leave a Reply

Your email address will not be published.