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 March 8, 2016)
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in ...Discover More
Macros are often used to process the data in a worksheet. If that data includes dates that need to be processed, you'll ...Discover More
When you use SUM to determine the total of a range of values, Excel doesn't really pay attention to whether the values ...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.