In this VBA Tutorial, you **learn how to convert strings to numbers** of the Byte, Integer, Long, Single, Double, Currency and Decimal data types.

This VBA Tutorial is accompanied by an Excel workbook containing the macros, data and formulas I use in the examples below. You can **get immediate access to this example workbook by subscribing to the Power Spreadsheets Newsletter**.

Use the following Table of Contents to navigate to the section that interests you.

## Related VBA and Macro Tutorials

The following VBA and Macro Tutorials may help you better understand and implement the contents below:

- General VBA constructs and structures:

- Tutorials about other useful topics:

- Learn how to work with the IFERROR worksheet function here.

You can find additional VBA and Macro Tutorials in the Archives.

## #1: Convert String to Byte

### VBA code to convert String to Byte

To **convert a string to a number of the Byte data type**, use the following statement:

CByte(String)

### Process followed by VBA to convert String to Byte

To **convert a string to a number of the Byte data type**, use the CByte function to convert the String to a number of the Byte data type.

### VBA statement explanation

**Item:**CByte.

**VBA construct:**CByte function.**Description:**The CByte function coerces String to the Byte data type.

CByte is usually able to carry out internationally-aware conversions from the String to the Byte data type. In other words, CByte generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The**Byte data type can hold numbers ranging from 0 to 255**. If String is outside this range, an error occurs.

**Item:**String.

**VBA construct:**String expression and expression argument of the CByte function.**Description:**String is the string or numeric expression you convert to the Byte data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Byte

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Byte.

Function stringToByte(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Byte 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Byte stringToByte = CByte(myString) End Function

### Effects of executing macro example to convert String to Byte

The following image illustrates the results of using the macro example in a worksheet formula. For these purposes:

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Byte, the worksheet formula returns the #VALUE! error. - Column C displays the worksheet formula used in column B.

## #2: Convert String to Integer

### VBA code to convert String to Integer

To **convert a string to a number of the Integer data type**, use the following statement:

CInt(String)

### Process followed by VBA to convert String to Integer

To **convert a string to a number of the Integer data type**, use the CInt function to convert the String to a number of the Integer data type.

### VBA statement explanation

**Item:**CInt.

**VBA construct:**CInt function.**Description:**The CInt function coerces String to the Integer data type.

If String contains a fraction,**CInt rounds it**. If this fraction is precisely 0.5, CInt rounds to the nearest even number. For example:

- 0.5 is rounded to 0.
- Both 1.5 and 2.5 are rounded to 2.
- Both 3.5 and 4.5 are rounded to 4.

- 0.5 is rounded to 0.
- CInt is usually able to carry out internationally-aware conversions from the String to the Integer data type. In other words, CInt generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The**Integer data type can hold numbers ranging from -32,768 to 32,767**. If String is outside this range, an error occurs.

**Item:**String.

**VBA construct:**String expression and expression argument of the CInt function.**Description:**String is the string or numeric expression you convert to the Integer data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Integer

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Integer.

Function stringToInteger(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Integer 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Integer stringToInteger = CInt(myString) End Function

### Effects of executing macro example to convert String to Integer

The following image illustrates the results of using the macro example in a worksheet formula. For these purposes:

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Integer, the worksheet formula returns the #VALUE! error. Additionally, if String contains a fraction that's precisely 0.5, the UDF rounds to the nearest even number. - Column C displays the worksheet formula used in column B.

## #3: Convert String to Long

### VBA code to convert String to Long

To **convert a string to a number of the Long data type**, use the following statement:

CLng(String)

### Process followed by VBA to convert String to Long

To **convert a string to a number of the Long data type**, use the CLng function to convert the String to a number of the Integer data type.

### VBA statement explanation

**Item:**CLng.

**VBA construct:**CLng function.**Description:**The CLng function coerces String to the Long data type.

If String contains a fraction,**CLng rounds it**. If this fraction is precisely 0.5, CLng rounds to the nearest even number. For example:

- 0.5 is rounded to 0.
- Both 1.5 and 2.5 are rounded to 2.
- Both 3.5 and 4.5 are rounded to 4.

CLng is usually able to carry out internationally-aware conversions from the String to the Long data type. In other words, CLng generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The**Long data type can hold numbers ranging from -2,147,483,648 to 2,147,483,647**. If String is outside this range, an error occurs.- 0.5 is rounded to 0.

**Item:**String.

**VBA construct:**String expression and expression argument of the CLng function.**Description:**String is the string or numeric expression you convert to the Long data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Long

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Long.

Function stringToLong(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Long 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Long stringToLong = CLng(myString) End Function

### Effects of executing macro example to convert String to Long

The following image illustrates the results of using the macro example in a worksheet formula. For these purposes:

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Long, the worksheet formula returns the #VALUE! error. Additionally, if String contains a fraction that's precisely 0.5, the UDF rounds to the nearest even number. - Column C displays the worksheet formula used in column B.

## #4: Convert String to Single

### VBA code to convert String to Single

To **convert a string to a number of the Single data type**, use the following statement:

CSng(String)

### Process followed by VBA to convert String to Single

To **convert a string to a number of the Single data type**, use the CSng function to convert the String to a number of the Single data type.

### VBA statement explanation

**Item:**CSng.

**VBA construct:**CSng function.**Description:**The CSng function coerces String to the Single data type.

CSng is usually able to carry out internationally-aware conversions from the String to the Single data type. In other words, CSng generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The**Single data type can hold floating-point numbers ranging from**:

- -3.402823E38 to -1.401298E-45 for negative values; and
- 1.401298E-45 to 3.402823E38 for positive values.

If String is outside the required range, an error occurs.

- -3.402823E38 to -1.401298E-45 for negative values; and

**Item:**String.

**VBA construct:**String expression and expression argument of the CSng function.**Description:**String is the string or numeric expression you convert to the Single data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Single

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Single.

Function stringToSingle(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Single 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Single stringToSingle = CSng(myString) End Function

### Effects of executing macro example to convert String to Single

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Single, the worksheet formula returns the #VALUE! error. - Column C displays the worksheet formula used in column B.

## #5: Convert String to Double

### VBA code to convert String to Double

To **convert a string to a number of the Double data type**, use the following statement:

CDbl(String)

### Process followed by VBA to convert String to Double

To **convert a string to a number of the Double data type**, use the CDbl to convert the String to a number of the Double data type.

### VBA statement explanation

**Item:**CDbl.

**VBA construct:**CDbl function.**Description:**The CDbl function coerces String to the Double data type.

CDbl is usually able to carry out internationally-aware conversions from the String to the Double data type. In other words, CDbl generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The**Double data type can hold floating-point numbers ranging from**:

- -1.79769313486231E308 to -4.94065645841247E-324 for negative values; and
- 4.94065645841247E-324 to 1.79769313486232E308 for positive values.

If String is outside the required range, an error occurs.

- -1.79769313486231E308 to -4.94065645841247E-324 for negative values; and

**Item:**String.

**VBA construct:**String expression and expression argument of the CDbl function.**Description:**String is the string or numeric expression you convert to the Double data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Double

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Double.

Function stringToDouble(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Double 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Double stringToDouble = CDbl(myString) End Function

### Effects of executing macro example to convert String to Double

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Double, the worksheet formula returns the #VALUE! error. - Column C displays the worksheet formula used in column B.

## #6: Convert String to Currency

### VBA code to convert String to Currency

To **convert a string to a number of the Currency data type**, use the following statement:

CCur(String)

### Process followed by VBA to convert String to Currency

To **convert a string to a number of the Currency data type**, use the CCur to convert the String to a number of the Currency data type.

### VBA statement explanation

**Item:**CCur.

**VBA construct:**CCur function.**Description:**The CCur function coerces String to the Currency data type.

CCur is usually able to carry out internationally-aware conversions from the String to the Currency data type. In other words, CCur generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The Currency data type holds integers scaled by 10,000. This results in Currency holding fixed-point numbers with 15 digits to the left of the decimal point and 4 digits to the right of the decimal point. Therefore,**Currency can hold numbers ranging from -922,337,203,685,477.5808 to 922,337,203,685,477.5807**. If String is outside this range, an error occurs.

**Item:**String.

**VBA construct:**String expression and expression argument of the CCur function.**Description:**String is the string or numeric expression you convert to the Currency data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Currency

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Currency.

Function stringToCurrency(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Currency 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Currency stringToCurrency = CCur(myString) End Function

### Effects of executing macro example to convert String to Currency

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Currency, the worksheet formula returns the #VALUE! error. - Column C displays the worksheet formula used in column B.

## #7: Convert String to Decimal

### VBA code to convert String to Decimal

To **convert a string to a number of the Decimal data type**, use the following statement:

CDec(String)

### Process followed by VBA to convert String to Decimal

To **convert a string to a number of the Decimal data type**, use the CDec function to convert the String to a number of the Decimal data type.

### VBA statement explanation

**Item:**CDec.

**VBA construct:**CDec function.**Description:**From a broad perspective, the CDec function coerces String to the Decimal data subtype of the Variant data type. In other words, CDec doesn't return a discrete data type, but rather a Variant converted to the Decimal subtype.

CDec is usually able to carry out internationally-aware conversions from the String to the Decimal data type. In other words, CDec generally recognizes between the different decimal/thousand separators and currency options that depend on your computer's locale.

The Decimal data type holds integers scaled by a variable power of 10. This power of 10 specifies the number of digits to the right of the decimal point. Therefore, the**value ranges Decimal can hold are as follows**:

**When working with a scale of 0, which results in no decimal places:**-79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335.**When working with 28 decimal places:**

- The largest and smallest values are +7.9228162514264337593543950335 and -7.9228162514264337593543950335.
- The smallest non-zero values are -0.0000000000000000000000000001 and 0.0000000000000000000000000001.

- The largest and smallest values are +7.9228162514264337593543950335 and -7.9228162514264337593543950335.

If String is outside the required range, an error occurs.

**Item:**String.

**VBA construct:**String expression and expression argument of the CDec function.**Description:**String is the string or numeric expression you convert to the CDec data type. If you explicitly declare a variable to represent String, use the Variant data type.

### Macro example to convert String to Decimal

The following macro example, a User-Defined Function, converts a string passed as argument (myString) to Decimal.

Function stringToDecimal(myString As Variant) 'source: https://powerspreadsheets.com/ 'converts String to Decimal 'for further information: https://powerspreadsheets.com/vba-string-to-number/ 'convert String to Decimal stringToDecimal = CDec(myString) End Function

### Effects of executing macro example to convert String to Decimal

- Column A contains a numeric string.
- Column B contains a worksheet formula that uses the UDF example.

When String is outside the range of Decimal, the worksheet formula returns the #VALUE! error. - Column C displays the worksheet formula used in column B.