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.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...Discover More
Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...Discover More
When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...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.