Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Convert String to Currency

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To convert a text string to a currency value in Excel VBA, use the CCur function: Dim amount As Currency: amount = CCur("1,234.56"). This works when the string is a recognizable number in your system's regional format. If the string contains symbols or non‑standard separators, clean it first with Replace and then apply CCur. The result is a Currency data type, which stores four decimal places and is ideal for money calculations.

What You'll Need

  • Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
  • Basic familiarity with the VBA editor (Alt+F11).
  • A worksheet containing text values that look like numbers, or a way to create test strings.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (or use Developer > Visual Basic). Insert a new module: Insert > Module.
  2. Write a simple conversion procedure. Start with a string that is already in a standard numeric format, for example "1234.56". Use CCur to convert it:
    Sub ConvertStringToCurrency()
        Dim textValue As String
        Dim currencyValue As Currency
        textValue = "1234.56"
        currencyValue = CCur(textValue)
        MsgBox currencyValue
    End Sub
    Run it with F5. The message box should show 1234.56. If it shows an error, the string is not recognized as a number in your regional settings.
  3. Handle strings with currency symbols or thousands separators. CCur does not accept symbols like $, €, or commas used as thousands separators in all locales. Remove them first:
    textValue = "$1,234.56"
    textValue = Replace(textValue, "$", "")
    textValue = Replace(textValue, ",", "")
    currencyValue = CCur(textValue)
    This works for many cases, but be careful: in some European locales the comma is the decimal separator, so removing all commas would break the number. Adjust the replacements to match your regional format.
  4. Use Val as a fallback for mixed strings. If the string contains extra text (e.g., "USD 1234.56"), Val extracts the leading numeric part: currencyValue = CCur(Val(textValue)). However, Val stops at the first non‑numeric character, so it may return 0 if the string starts with letters. Clean the string first or use Mid to extract the numeric portion.
  5. Convert a range of cells. To process many cells, loop through a range and write the results to another column:
    Sub ConvertRange()
        Dim cell As Range
        For Each cell In Range("A1:A10")
            If IsNumeric(cell.Value) Then
                cell.Offset(0, 1).Value = CCur(cell.Value)
            Else
                cell.Offset(0, 1).Value = "Invalid"
            End If
        Next cell
    End Sub
    This checks each cell with IsNumeric before converting, avoiding errors on blank or text cells.
  6. Format the result as currency. The Currency data type stores the value, but the cell display depends on number formatting. Set the NumberFormat property:
    cell.Offset(0, 1).NumberFormat = "$#,##0.00"
    Adjust the format string to your local currency symbol and separator style.
  7. Test with edge cases. Try strings like "0", "-45.67", "1,000", and "abc". Ensure your code handles negatives, zero, and invalid input gracefully (e.g., using On Error Resume Next or IsNumeric checks).

Common Mistakes

  • Assuming CCur ignores currency symbols. It does not; you must strip them first.
  • Using CDbl or CSng instead of CCur. Those return floating‑point types, which can introduce rounding errors in money calculations. CCur is designed for currency.
  • Forgetting regional settings. A string like "1,234.56" may be interpreted differently on a system where comma is the decimal separator. Always test on the target machine or use explicit parsing.
  • Not checking for empty strings. CCur("") raises a type mismatch error. Use If Len(Trim(textValue)) > 0 Then before converting.
  • Overlooking negative values in parentheses. Accounting formats sometimes show negatives as "(123.45)". CCur will not parse that; you need to detect and convert manually.

Tips

  • Use IsNumeric to test if a string can be converted before calling CCur. It returns True for strings that look like numbers in the current locale.
  • For consistent behavior across locales, consider using Application.WorksheetFunction.NumberValue (Excel 2013+) to parse with explicit decimal and thousands separators.
  • When writing converted values back to cells, set the cell's NumberFormat to a currency format so the display matches the data type.
  • If you need to store the result as a string with currency formatting, use Format(currencyValue, "Currency") or a custom format string.
  • Always validate input: trim spaces, remove non‑breaking spaces (Chr(160)), and handle multiple currency symbols if your data is messy.

Troubleshooting

If CCur throws a "Type mismatch" error, the string is not recognized as a number. Check for leading/trailing spaces, currency symbols, or letters. Use Trim and Replace to clean the string. If the error persists, test with a simple numeric string like "123" to confirm the function works in your environment. If the converted value is wrong (e.g., 1.234,56 becomes 1.234), your regional settings may use comma as decimal separator; adjust your cleaning logic accordingly. For ranges, ensure you are not trying to convert cells that contain formulas returning errors; check with IsError first. If the code runs but the cell shows a date or other format, set the NumberFormat explicitly. If you get an "Overflow" error, the number is too large for the Currency type (range roughly -922,337,203,685,477.5808 to 922,337,203,685,477.5807); use CDec for larger values, but note CDec is not a native data type and has its own limits.

Frequently Asked Questions

Can I convert a string with a currency symbol directly using CCur?

No, CCur does not accept currency symbols. Remove them with Replace before converting.

What is the difference between CCur and CDbl?

CCur returns a Currency data type with fixed‑point arithmetic and four decimal places, suitable for money. CDbl returns a Double floating‑point number, which can cause rounding errors in financial calculations.

How do I handle strings with thousands separators like "1,234.56"?

Remove the separators with Replace(text, ",", "") if your locale uses comma as thousands separator. If your locale uses comma as decimal separator, adjust accordingly.

Why does CCur("1,23") return 123 in some systems?

Because in locales where comma is the decimal separator, "1,23" is interpreted as 1.23, but if the system expects a period as decimal, it may treat the comma as a thousands separator and return 123. Always test with your regional settings.

Can I convert an entire column of text to currency in one go?

Yes, loop through the range and apply CCur to each cell after validating with IsNumeric. Write the results to adjacent cells or overwrite the original cells.

What if the string contains non‑breaking spaces?

Use Replace(text, Chr(160), "") to remove them before conversion.