To get or force a numeric value in Excel VBA, use the IsNumeric function to test whether a value can be read as a number, then coerce it with CDbl, CLng, Val or Range.Value2 so arithmetic actually works. The reason this matters is that Excel stores a number typed as text as text: it looks right in the cell but +, Sum and comparisons behave strangely. VBA has its own set of functions for turning strings, variants and cell contents into real numbers, and once you know which one to reach for, the problem disappears in a few lines of code. The steps below cover testing a value, converting it safely, reading numbers from cells, and formatting the result without losing precision.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled; if it is missing, right-click the ribbon and choose to customize it, or open the Trust Center settings and enable macros for the file's location only.
- A workbook saved as .xlsm, .xlsb or .xlam — a standard .xlsx file cannot store macro code.
- A basic familiarity with the VBA editor (Alt+F11 on Windows, or the Developer tab on Mac) and with adding a standard module.
- A test sheet containing some deliberately awkward data: numbers stored as text, entries with spaces, entries with currency symbols, and one genuinely non-numeric cell.
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11 on Windows (on Mac, use the Developer tab, then Visual Basic), then choose Insert > Module. Always work in a standard module rather than a sheet module unless the code is meant to react to events. Menu wording can differ slightly between Excel versions and between desktop and Mac, but Insert > Module is present in all current versions.
- Declare your variables explicitly. At the top of the module choose option explicit is on, and declare every variable with a specific type —
Dim v As Variant,Dim d As Double. A Variant can hold a number, a date, an error or a string, and many conversion problems come from assuming which one you have. A Double handles decimals and large values; a Long is better for whole numbers below about two billion. - Test before converting. Use
IsNumeric(v)to ask VBA whether the value can be treated as a number. It returns True for numbers, numeric strings such as "123", strings with a leading sign or decimal point, and also for dates, because VBA stores dates as serial numbers. That last point matters: if a cell contains a date,IsNumericreturns True andCDblgives you the serial number, not the day you expected. Test the cell's format as well if dates are possible. - Convert with the right function.
CDbl(v)converts to a Double and is the safest general choice for decimals.CLng(v)rounds to a whole number and will raise an error if the value is out of range.CInt(v)does the same but is limited to smaller integers.CSng(v)trades precision for memory. Wrap any of them in error handling because a failed conversion stops the macro with a type mismatch. - Understand what Val does differently.
Val("£1,234.50")stops reading at the first character it does not recognise, so the currency symbol produces zero. Stripping symbols and thousands separators first, or usingCDblafter cleaning, gives the right result.Valnever raises an error, which is useful for messy imports but means silent wrong answers are possible. - Read from cells using Value2 when you need raw numbers.
Range("A1").Valuereturns a formatted date or currency value in some situations, while.Value2returns the underlying serial or number. For arithmetic,Value2is often the cleaner choice. If the cell holds text, both return a string, and that is exactly the case the conversion functions exist for. - Handle text numbers in bulk. Loop the used range and test each cell:
If IsNumeric(cell.Value2) Then cell.Value2 = CDbl(cell.Value2). This rewrites the cell as a true number without touching formatting, so a trailing apostrophe or a text-formatted column stops breaking totals. Limit the loop to the actual data range — looping an entire column of over a million rows is slow. - Deal with the common culprits explicitly. Trim spaces with
Trim, remove non-breaking spaces withReplace(s, Chr(160), ""), replace commas used as thousands separators, and convert a comma decimal separator to a period if your data comes from a region that uses it. In VBA the functions respect the system locale, so a string like "1,5" may convert as fifteen hundred in one region and one and a half in another. Test on the machine that will run the macro. - Control the output format. Once you have a real number, set the display with
NumberFormatrather than converting back to text. To build a string for a message box, useFormat(d, "0.00")so the result is predictable regardless of the user's regional settings. Never store formatted text back into a numeric column — that recreates the original problem. - Test with a mix of inputs and then step through. Run the macro on your test sheet, then step through it with F8 and check each value in the Locals window. Confirm the totals match a hand calculation before running it on live data.
Common Mistakes
- Assuming a cell that looks numeric is numeric. The alignment is a hint, not proof — check with
IsNumericorTypeName. - Using
CIntorCLngon decimals and expecting the decimal part to survive; these round. - Forgetting that
IsNumericaccepts dates, so date cells get converted to serial numbers and then displayed as nonsense. - Converting to text at the end with
CStrand putting it back into the cell, which undoes the whole exercise. - Ignoring locale differences. A macro that works on one machine can silently misread decimal separators on another.
- Leaving
Option Explicitoff, so a mistyped variable becomes an empty Variant and converts to zero without any warning.
Tips
- Keep conversion and presentation separate: store numbers as numbers, format with
NumberFormat, and only turn them into text when building a message or report string. - Write a small helper function that trims, strips currency symbols and separators, then converts, and reuse it everywhere instead of repeating the logic.
- Use
TypeName(v)while debugging to see exactly what VBA thinks the value is — "String", "Double", "Date", "Empty" or "Error". - For imported data, fix the source: Power Query can set column types on import, which is more reliable than patching cells afterward.
- Test with values at the edges — very large numbers, negatives, blank cells and a genuine word — not just tidy examples.
Troubleshooting
Type mismatch error. The value cannot be converted with the function you chose. Wrap the conversion in On Error handling or guard it with IsNumeric, and check the cell for hidden spaces or symbols first.
Results are zero. Val hit a non-numeric character immediately, or the cell is empty. Inspect the raw string; a leading apostrophe or a non-breaking space from a web copy is a frequent cause.
Decimals are wrong by a factor of a hundred or a thousand. This is almost always a separator problem. Confirm whether the data uses a comma or period as the decimal mark, and clean it before converting.
Run-time error about overflow. The number is too big for the type. Switch from CInt or CLng to CDbl.
The macro reports success but totals are still off. The loop may have skipped text cells because your test rejected them, or it wrote formatted text back. Re-check what is actually stored with Value2 and TypeName. If you are still stuck, copy the awkward column into a fresh sheet and convert it manually to see the pattern.
Frequently Asked Questions
How do I check whether a cell contains a number in VBA?
Use IsNumeric(Range("A1").Value2). It returns True for real numbers and numeric text, but also for dates, so check the cell format if dates are possible.
What is the difference between Val and CDbl?
Val reads characters left to right and stops at the first non-numeric one, never raising an error. CDbl converts the whole value and raises an error if it cannot, which makes problems visible instead of silent.
Why does converting a price with a currency symbol give zero?
The symbol is not a number, so the read stops before any digits. Remove the symbol and thousands separators first, then convert the remaining digits.
Should I use Value or Value2?
For arithmetic on raw numbers, Value2 is usually safer because it returns the underlying number without date or currency formatting applied.
Can VBA convert text numbers in an entire column at once?
Not without a loop or a range-wide operation. Loop the used range only, test each cell, and write the converted value back as a number.
Why does the same macro give different decimals on another computer?
VBA conversion functions follow the system's regional settings. A comma decimal separator is read differently depending on the locale, so normalise separators before converting.