Search notes:

VBA: data type conversion functions

The data type conversion functions of VBA are: cBool, cByte, cCur, cDate, cDec, cDbl, chr, cInt, cLng, cLngLng, cLngPtr, cSng, cStr, cVar and cvErr.
strConv converts from one string data type to another string data type.

Numerical conversion functions

The numerical conversion functions cDec, cInt, cLng etc. convert a string to the corresponding datatype.
If the string starts or ends with a minus sign, the returned value is negative, too:
debug.print(cInt("42-"))
-42
debug.print(cDbl("-8.42"))
-8.42

cDec

Although VBA doesn't have a decimal datatype, a string value (such as "42.1234") can be converted to a decimal with cDec.

cLng

option explicit

sub main()

  ' Spaces around the number are possible
    debug.print cLng("  123  "  )
    
  ' Not possible, type missmatch error
  ' debug.print cLng("  456 xyz")
  
  ' With the &h prefix, the following text is interpreted as
  ' a number's hexadecimal representation.
  ' The following prints 42.
    debug.print cLng("&h2a"     )
    
  ' 17.3 is rounded down to 17
    debug.print cLng("17.3")
    
  ' 17.7 is rounded up to 18
    debug.print cLng("17.7")

end sub
Github repository about-VBA, path: /functions/cLng.bas

Misc

Although VBA doesn't have a decimal datatype, a string value (such as "42.1234") can be converted to a decimal with cDec.
Arguably, the functions asc, hex, oct, str and val might be considered conversion functions, too.

See also

Other VBA functions

Index