Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Replacing and Converting in a Macro.
by Allen Wyatt
(last updated December 20, 2019)
Saskia was having a problem converting information, under the control of a macro, and still having it be usable in Excel. When she would receive a worksheet that showed numbers formatted with decimal points, she would need to convert the values so they used decimal commas, consistent with how numbers are displayed in Holland. She would do a find and replace, and everything would work fine. However, when she recorded a macro that did the find and replace, the resulting cells were treated as text instead of as numeric values.
The reason for this behavior is that Excel VBA "speaks" American, and some actions done using a recorded macro don't work as expected due to that fact. Because American Excel expects the decimal separator to be a period, interpreting a "number" in VBA with another separator (such as a comma) will cause Excel to consider the value to be text.
The workaround is not to use find and replace, but to use a different trick. Consider the following short macro:
Sub ConvertNumbers() Dim oConRange As Range Set oConRange = ActiveSheet.UsedRange.Cells.SpecialCells(xlConstants) oConRange.Value = oConRange.Value End Sub
This macro defines a range that consists of all the cells that contain constants. Then, it sets the value of each cell in the range equal to itself. In the process of doing this, Excel re-evaluates the contents of each cell and converts it to the appropriate numeric value. In other words, numbers that contain decimal points are converted to numbers that contain decimal commas.
There are other ways you can process the cells using a macro, but the above procedure seems to work the best and the quickest.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12630) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Replacing and Converting in a Macro.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
If you need to know whether a particular value is odd or even, you can use this simple formula. Designed to be used in a ...Discover More
Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.Discover More
Does your macro need to allow the user to specify a particular file name that should be used by the macro? Here's a quick ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.