Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Replacing and Converting in a Macro.
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 (2291) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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!