Home » excel » excel – Replace "." with "/" macro in vba change order of numbers

excel – Replace "." with "/" macro in vba change order of numbers

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to change “.” with “/” with a macro in Excel vba, it works, but when I activate the macro it changes the value of the cell from 10.11.2016 to 11/10/2016.
Why does it change the order? The cell doesn’t have any format.

Range("D1").Select
Selection.Replace What:=".", Replacement:="/", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
How to&Answers:

The cell doesn’t have the format but it is mirroring the format from the regional settings. Check Control Panel | Clock and Region | Region. The moment you replace the text, it becomes a date and then it simply picks up the format from regional settings.

Try this

With Range("D1")
    .NumberFormat = "@"
    .Value = Replace(.Value, ".", "/")
End With

What I am doing here is converting the format of the cell to text and then replacing the characters.

Great, it works! But can I also replace “.” with “/” in the entire column D with this method without using a loop? If yes how? Thank you – Masca2056 7 mins ago

Try this. I am assuming 10 is the last row.

Sub Sample()
    Columns(4).NumberFormat = "@"
    [D1:D10] = [INDEX(SUBSTITUTE(D1:D10,".","/"),)]
End Sub

See this LINK for an explanation of using the above method.