by Allen Wyatt
(last updated September 14, 2019)
Jean needs to replace commas with periods in a table of numbers. He works in France, using a French PC, but he needs to "translate" these numbers for a non-Excel American application. When he manually does a Find and Replace, everything works well. Of course, his numbers are converted to strings, but that's OK with Jean. When he tries to do the same Find and Replace operation in a macro, nothing happens; the commas seem to be ignored. Jean wonders how he can do this task in a macro rather than needing to do it manually each time.
It is relatively easy to temporarily modify what Excel uses as regional settings so that it can display information just as Jean wants. The following macro will toggle back and forth rather handily between the desired settings:
Sub ChangeNumberFormat() With Application .UseSystemSeparators = False If .DecimalSeparator = "." Then .DecimalSeparator = "," .ThousandsSeparator = "." Else .DecimalSeparator = "." .ThousandsSeparator = "," End If End With End Sub
That changes how the information is displayed, and then you could export the information to whatever non-Excel format desired (such as CSV). Once done, run the macro again and you should have your regional settings back to the way they were originally. The benefit to this approach is that you never really change what is in the cells, just how that information is displayed. This means that your numbers remain numbers, able to continue to be used as such.
If you need something more direct, then it is probably best not to use Find and Replace. Instead, use the macro to directly modify what is in each cell. Such an approach can run much faster than relying on Find and Replace, even in a macro. Here's a simple application of this concept:
Sub SwitchCommasPeriods() Dim c As Range Dim sTemp As String For Each c In Selection sTemp = c.Text sTemp = Replace(sTemp, ",", "|") sTemp = Replace(sTemp, ".", ",") sTemp = Replace(sTemp, "|", ".") c = sTemp Next c End Sub
Note a couple of things in the macro. First, it looks at the Text property for each cell. The reason is because the Text property returns the formatted value of the cell—in other words, what is displayed. (If you simply look at the Value property, you'll get the unformatted number, which is not what you want to use.)
The second thing to notice is that the Replace function is used to first replace all commas with a vertical bar, then all periods with commas, and finally all vertical bars with periods. This approach is necessary because you are essentially swapping periods and commas in the text.
To use the macro, simply select the cells you want to affect and then run it. Only those selected cells are processed. If a cell contains a formula, the macro replaces that formula with the processed result of the formula. This should be fine for Jean's purposes, however.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13675) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...Discover More
Tracking down memory errors in a macro can be frustrating. The error message is inherently vague and correcting any ...Discover More
Want a quick way to jump to the end of your data entry area in a worksheet? The macro in this tip makes quick work of the ...Discover More
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.