Replacing Commas with Periods

Written by Allen Wyatt (last updated July 3, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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 Microsoft 365.

Author Bio

Allen Wyatt

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. ...

MORE FROM ALLEN

Deleting Menu Items

Excel allows you to customize your menus so that they contain the commands you want on them. If you later want to delete ...

Discover More

Disabling a Function Key

Function keys are used to perform common tasks in Excel. If you want to disable one of the function keys, it's rather ...

Discover More

Sizing Text Boxes and Cells the Same

Adding a text box to a worksheet is easy. Making sure that text box is the exact size of a cell in the worksheet may not ...

Discover More

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!

More ExcelTips (ribbon)

Continuing Macro Lines

Sometimes a macro command line can get very, very long. This can make it hard to understand when you look at it a month ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Running a Macro in a Number of Workbooks

Got a macro that you need to run on each of a number of workbooks? Excel provides a number of ways to go about this task, ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2019-09-22 09:42:06

JMJ

Thanks very much to Allen and J. Wooley for this clever solution!


2019-09-14 10:23:12

J. Woolley

Allen's first macro can be simplified:

Sub ChangeNumberFormat()
With Application
.DecimalSeparator = "."
.ThousandsSeparator = ","
.UseSystemSeparators = (Not .UseSystemSeparators)
End With
End Sub

The first time it is run all numbers will change from French (or default) to American:
123 456 789,12 becomes 123,456,789.12
The next time it is run all numbers will change back to French (or default).
Each time it is run the separators will change from their system default to American or vice versa.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.