Converting Strings


String to Number

Before trying to convert a string to a number it is a good idea to always make sure that the value is a numerical value.
The ISNUMERIC function will return True or False depending if the value can be evaluated as a number.
The CINT function converts a String to an Integer data type.

Dim sMyString As String 
Dim iNumber As Integer
sMyString = "200"
If (IsNumeric(sMyString) = True) Then
   iNumber = CInt(sMyString)
   Debug.Print 10 * iNumber
End If

The CDBL function converts a String to a Double data type.
The CLNG function converts a String to a Long data type.
The CSNG function converts a String to a Single data type.


String to Date

The CDATE function converts a String to a Date data type.

Dim sMyString As String 
Dim dtDate As Date
sMyString = "21/6/2023"
If (IsDate(sMyString) = True) Then
   dtDate = CDate(sMyString)
   Debug.Print dtDate
End If

Number to String

The CSTR function converts a Number to a String data type.

Dim sMyString As String 
Dim lNumber As Long
lNumber = 400
sMyString = CStr(lNumber)
Debug.Print "My String is " + sMyString

Date to String

The CSTR function converts a Date to a String data type.
The DATESERIAL function returns a date given a year, month and day.

Dim sMyString As String 
Dim dtDate As Date
dtDate = DateSerial(2023,7,7)
sMyString = CStr(dtDate)
Debug.Print "My String is " + sMyString

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext