Convert String to Int in VBA: CInt, Val, and More

VBA & macros · Updated November 23, 2026

To convert a string to an integer in VBA, use the CInt() function for most cases, or Val() when you need to handle leading numbers or non-numeric characters gracefully. For example, CInt("42") returns the integer 42, while Val("42abc") returns 42 (ignoring the trailing letters).

Why Conversion Matters in VBA

VBA is a loosely typed language, but variables declared as Integer or Long store numbers, not text. When you read data from a worksheet cell, a text box, or a CSV file, the value often arrives as a String. Arithmetic operations like + or * on a string that looks like a number will either produce a type mismatch error or concatenate instead of add. Converting explicitly avoids these errors and makes your code predictable.

Consider a common scenario: you have a cell with “123” and you try result = myString + 1. If myString is a variant holding text, VBA may coerce it to a number, but if the string contains spaces or a currency symbol, it fails. Using CInt forces the conversion and raises a clear error if the input isn’t numeric.

The Main Conversion Functions

VBA offers several functions, each with different behavior:

  • CInt(expression) – Converts to an Integer (16-bit, range -32,768 to 32,767). Rounds to nearest whole number (banker’s rounding).
  • CLng(expression) – Converts to a Long (32-bit, range about ±2.1 billion). Use this for larger numbers.
  • Val(string) – Returns a Double (floating-point) from the leading numeric portion of a string. Ignores non-numeric characters after the first number. Does not throw an error on invalid input; returns 0 if no number is found.
  • CDbl(expression) – Converts to a Double (floating-point). Useful when you need decimals.
  • CStr(expression) – Converts a number to a string (the reverse operation).

For a pure integer conversion, CInt and CLng are the standard choices. Val is more forgiving but returns a Double, so if you need an integer, you must round or truncate it yourself.

How to Choose the Right Function

Pick based on your data and the required output type:

  • If you know the string is a clean integer (no decimals, no extra characters), use CInt for small values or CLng for larger ones.
  • If the string might have leading spaces or trailing text (e.g., ” 12 items”), use Val to extract the number, then convert to integer with Int() or CLng if needed.
  • If the string could be empty or non-numeric and you want to avoid runtime errors, use IsNumeric() to check first, or use Val which returns 0 for invalid input.
  • If you need to handle decimal strings like “3.14” and want an integer, use CInt (rounds) or Int() (truncates). Be aware that CInt uses banker’s rounding: CInt("2.5") returns 2, not 3.

For most spreadsheet data, CLng is safer than CInt because it avoids overflow errors on numbers that exceed 32,767. Only use CInt when you are certain the value fits within the Integer range.

Step-by-Step Example with Error Handling

Here is a practical macro that reads a value from cell A1, converts it to an integer, and writes the result to B1. It includes error handling to avoid crashing on invalid input.

Sub ConvertStringToInt()
    Dim inputStr As String
    Dim result As Integer
    
    ' Read the cell value as a string
    inputStr = CStr(Sheet1.Range("A1").Value)
    
    ' Check if it's numeric before converting
    If IsNumeric(inputStr) Then
        result = CInt(inputStr)  ' Convert to Integer
        Sheet1.Range("B1").Value = result
    Else
        MsgBox "Cell A1 does not contain a valid number."
    End If
    
    ' Always save a copy before running macros - Ctrl+Z cannot undo a macro
End Sub

If A1 contains “123”, B1 gets 123. If A1 contains “abc”, you get a message box. If A1 contains “123.9”, CInt rounds to 124.

Common Pitfalls and Misconceptions

One frequent mistake is assuming Val returns an integer. It returns a Double, so Val("123.45") gives 123.45, not 123. If you assign that to an Integer variable, VBA rounds it, but that’s an implicit conversion.

Another pitfall is locale differences. CInt and Val respect the system’s decimal separator. In European locales, a comma is used for decimals, so CInt("1,5") returns 2 (rounded), while Val("1,5") returns 1 because it stops at the comma. Use CDbl with Replace if you need to handle a specific format.

Also, CInt throws an overflow error if the number is outside the Integer range. For example, CInt("40000") causes an error. Use CLng instead. Similarly, CInt on an empty string returns 0, but on a string with only spaces it returns 0 as well—this might be unexpected.

Comparing Val, CInt, and CLng

The table below summarizes the key differences:

  • Val(“123”) → 123 (Double). Stops at first non-numeric character.
  • Val(“123abc”) → 123 (Double). Ignores “abc”.
  • CInt(“123”) → 123 (Integer). Requires the entire string to be numeric.
  • CInt(“123.5”) → 124 (Integer) due to rounding.
  • CLng(“123456”) → 123456 (Long). Handles larger numbers.
  • CInt(“abc”) → Runtime error 13 (Type mismatch).

If you need to extract a number from a mixed string, Val is convenient. If you need strict validation, use IsNumeric before CInt.

Alternatives: Worksheet Functions and Type Conversion

In some cases, you can avoid VBA entirely. Excel’s worksheet functions like VALUE() or NUMBERVALUE() convert text to numbers, but they don’t work inside VBA without the WorksheetFunction object. For example, WorksheetFunction.Value("123") returns a double. However, for VBA-native code, the built-in functions above are simpler and faster.

Another approach is to use CDec for decimal conversion, but that returns a Decimal variant, not an integer. If you need to parse a string with thousands separators like “1,234”, you can remove commas first: CLng(Replace("1,234", ",", "")).

Frequently Asked Questions

Does CInt round or truncate? CInt rounds to the nearest even number (banker’s rounding). For example, CInt(“2.5”) returns 2, and CInt(“3.5”) returns 4. If you want to always round down, use Int() or Fix() after converting to a double.

What happens if the string is empty? CInt(“”) returns 0. Val(“”) also returns 0. However, if the string contains only spaces, CInt(” “) returns 0 as well, but IsNumeric(” “) returns False, so using IsNumeric first is safer.

Why do I get a type mismatch error? This occurs when the string contains non-numeric characters like “12a” or “12.3.4”. CInt and CLng require the entire string to be a valid number. Use Val to extract the leading number, or check with IsNumeric before converting.

Can I convert a string to a Long in VBA? Yes, use CLng. It works the same as CInt but with a larger range. For numbers beyond Long’s range, use CDbl or CDec.

Is there a way to convert without rounding? If you want to truncate the decimal part, use Int(CDbl(“12.7”)) which returns 12. CInt would round to 13. Use Fix() for negative numbers to truncate toward zero.

Does Val work with negative numbers? Yes, Val(“-123”) returns -123. It also handles leading spaces and tabs. However, Val stops at a second minus sign or at a letter, so Val(“-123abc”) returns -123.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.