VBA Function Convert Number to String: CStr & More

VBA & macros · Updated November 23, 2026

The quickest way to convert a number to a string in VBA is with the CStr() function, for example Dim s As String: s = CStr(123.45). CStr() converts any numeric expression to its String representation based on your system’s locale settings, but there are several other conversion functions and formatting methods you should know about for full control.

Why Convert a Number to a String in VBA?

You rarely need a standalone number as text, but string conversion becomes essential when you combine numbers with other text. Common scenarios include:

  • Building a message box or user-facing message that mixes numbers and labels, such as MsgBox "Total: " & CStr(total).
  • Creating file names or sheet names that include a numeric value, like "Report_" & CStr(Year(Date)).
  • Writing data to a text file or CSV where every field must be a string.
  • Concatenating numbers with other strings using the & operator—VBA automatically converts, but explicit conversion makes your intent clear.

Without conversion, VBA still concatenates numbers using &, but the result can be unexpected if you use the + operator, which may add numbers instead of joining them.

How CStr Works and What It Returns

CStr() is the standard, all-purpose conversion function. It accepts any numeric data type (Integer, Long, Single, Double, Currency, Decimal) and returns a String. The exact text depends on your system’s regional settings—for example, a decimal point might appear as a comma in some locales. For most internal use, that’s fine, but if you need a consistent format regardless of the user’s machine, use Format() instead.

Example:

Dim num As Double
Dim s As String
num = 1234.5
s = CStr(num) ' s = "1234.5" in US locale

Note that CStr() does not add thousands separators or trailing zeros. It gives the shortest representation that preserves the value.

Format Function for Full Control

When you need a specific number format—currency, percentages, leading zeros, or thousands separators—use the Format() function. It returns a string based on a format expression you supply.

Examples:

  • Format(1234.5, "#,##0.00") returns "1,234.50".
  • Format(0.25, "0%") returns "25%".
  • Format(7, "000") returns "007".
  • Format(1234.5, "Currency") returns a locale-specific currency string.

Unlike CStr(), Format() always uses the system’s locale for decimal symbols and currency, but the format string gives you control over the structure. For truly invariant output (e.g., for file names), you might combine Format() with Replace() to swap decimal separators.

Other Conversion Functions: Str, CInt, CLng, etc.

VBA has a family of conversion functions. For number-to-string, the relevant ones are:

  • Str(number) — Converts a number to a string, but always prepends a leading space for positive numbers (e.g., Str(123) returns " 123"). This is a legacy function; CStr() is preferred.
  • CStr(expression) — Converts any expression that can be evaluated to a string, including booleans and dates, not just numbers.
  • Format(expression, format) — As described above, gives full formatting control.

Be careful not to confuse these with CInt(), CLng(), CDbl(), etc., which convert strings to numbers, not the other way around.

Choosing the Right Method for Your Task

Here’s a quick decision guide:

  • Need a plain, no-frills string? Use CStr().
  • Need a specific number of decimal places, thousands separators, or a percentage? Use Format().
  • Need to avoid locale issues (e.g., always use a dot as decimal separator)? Use Format() with a custom format and then replace the decimal separator if necessary, or use Str() and trim the leading space.
  • Need to concatenate a number into a string? You can rely on implicit conversion with &, but explicit CStr() is clearer.

For most day-to-day code, CStr() is the best default because it’s concise and predictable.

Common Problems and Pitfalls

Several issues trip up VBA developers:

  • Locale differences: CStr() and Format() use the system’s decimal separator. If you’re generating strings for a file name or a data interchange, this can cause errors on computers with different regional settings. Solution: use a custom format with Format() and then replace the separator, or use Str() and trim the leading space.
  • Leading space from Str(): Str(123) returns " 123". If you forget to trim it, you’ll get unexpected spaces in your output. Use Trim(Str(123)) or just use CStr().
  • Overflow with CStr on very large numbers: CStr() can handle any numeric data type, but if you pass a Variant holding a Decimal with many digits, it may produce scientific notation. Use Format() with a custom format to force fixed notation.
  • Concatenation with + instead of &: Using + between a number and a string can cause a type mismatch error or numeric addition. Always use & for string concatenation.

Performance and Best Practices

Conversion functions are fast, so performance is rarely a concern. However, best practices include:

  • Always declare your variables with explicit types (Dim s As String) to avoid Variant overhead.
  • Use CStr() instead of Str() to avoid the leading space issue.
  • If you’re building a large string in a loop (e.g., generating a CSV), consider using the StringBuilder class from .NET via CreateObject("System.Text.StringBuilder") for better performance, though for most cases simple concatenation is fine.
  • Remember that VBA’s & operator automatically converts numbers to strings, so you can often skip explicit conversion. But explicit conversion improves readability and prevents accidental addition.

Frequently Asked Questions

What is the difference between CStr and Str in VBA?

Str() always adds a leading space for positive numbers, while CStr() does not. For example, Str(123) returns " 123" but CStr(123) returns "123". CStr() is generally preferred because it behaves more intuitively and also converts other data types like booleans and dates.

How do I convert a number to a string with leading zeros?

Use the Format() function with a custom format that includes zeros. For example, Format(7, "000") returns "007". The number of zeros in the format string determines the minimum length.

Why does my converted number show a comma instead of a decimal point?

This happens because your Windows regional settings use a comma as the decimal separator. CStr() and Format() respect the system locale. To force a dot, you can use Replace(CStr(num), ",", ".") or use Str() and trim, but the cleanest is to use Format(num, "0.00") and then replace the separator if needed.

Can I convert a number to a string without losing precision?

Yes, CStr() preserves the full precision of the numeric type. For floating-point numbers, the string representation may be in scientific notation for very large or very small values, but the value is not lost. If you need a fixed number of decimal places, use Format().

Is there a way to convert a number to a string in Excel VBA without using a function?

You can rely on implicit conversion with the & operator. For example, Dim s As String: s = 123 & "" works, but it’s less explicit and can be confusing. Using CStr() is clearer and avoids any ambiguity.

What if I need to convert a number to a string in a formula in Excel, not VBA?

In Excel formulas, you can use the TEXT() function, like =TEXT(A1, "0.00"). That’s separate from VBA, but if you’re writing VBA code that interacts with cells, you can use WorksheetFunction.Text or simply assign the value to a cell and read it back as text.

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.