简体   繁体   中英

Replacing commas with dots in Excel script

I want to replace a comma with dot and vice-versa

Public Sub VirgulaPunct()
Dim oRow As Range
Dim cell As Range
Dim i As Long, j As Long
Dim MyString As String
Dim aux As String

    Application.ScreenUpdating = False

    For i = Selection(Selection.Count).Row To Selection.Cells(1, 1).Row Step -1

        For j = Selection(Selection.Count).Column To Selection.Cells(1, 1).Column Step -1
            MyString = Cells(i, j).Value
            For Counter = 1 To Len(MyString)
                aux = Mid(MyString, Counter, 1)
                If aux = "." Then

                  MyString = Replace(MyString, ".", ",", 1)

                ElseIf aux = "," Then

                  MyString = Replace(MyString, ",", ".", 1)

                End If
            Next
            Cells(i, j).Value = MyString
        Next j
    Next i
    Application.ScreenUpdating = True
End Sub

The basic problem is that if the columns is set as general instead of text, even if we changed the "," for "." Excell automatically it will change it again for a ",".

To avoid that it is necessary to set first the column as a Text format, and then we can perform replace function:

This it works for me:

Worksheets("Name").Activate
Worksheets("Name").Columns(1).Select 'or Worksheets("SheetName").Range("A:A").Select
Selection.NumberFormat = "@"

Dim OriginalText As String
Dim CorrectedText As String

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

    For i = 1 To LastRow

    OriginalText = Worksheets("Name").Cells(i, 1).Value

    CorrectedText = Replace(OriginalText, ",", ".")

    Worksheets("Name").Cells(i, 1).Value = CorrectedText

    Next i

You can use the Replace method of the Range object

Sub ReplacePunct()

    Const sTEMPCOMMA = "|comma|"
    Const sTEMPDOT = "|dot|"
    Const sCOMMA = ","
    Const sDOT = "."

    If TypeName(Selection) = "Range" Then
        With Selection
            .Replace sCOMMA, sTEMPCOMMA, xlPart
            .Replace sDOT, sTEMPDOT, xlPart
            .Replace sTEMPCOMMA, sDOT, xlPart
            .Replace sTEMPDOT, sCOMMA, xlPart
        End With
    End If

End Sub

I have tried the same thing, but I get unexpected results. I can do what I want with a manual find-and-replace method. But when I try to automate this with VBA, I get weird formatting issues. I have tried the solutions suggested here, but the same issues persist.

My solution is to use the built-in NUMBERVALUE function.

Here is a solution that changes a dot to comma. It can easily be adapted to work the other way around.

Sub ChangeToComma()

Dim c As Range
Dim num As Double

For Each c In Selection
    num = Application.WorksheetFunction.NumberValue(c.Value, ".", ",")
    c.Value = num
Next

End Sub

By looking at your loops you don't seem that you are visiting any cell more than once. Wouldn't a simple if statement work here ?

if(Cells(i,j).Value = ',')
  Cells(i,j).Value = '.'
Elsif(Cells(i,j).Value = '.')
  Cells(i,j).Value = ',')
End

Coming at it from a VB perspective rather than VBA, I'd look at using the builtin Replace function rather than looping through it.

So first I'd replace all , with ; (or if there could be ; in the text I'd use something else, possibly a combination of characters, like ;+;+ or similar), then replace all . with , and finally replace all ; with . .
Probably not very efficient, but very easy.

This seems to be a continuation to my answer to your previous question, but if so I think you misunderstood what I meant. I've taken your code and amended it with my suggestion, but I've not tested it:

Public Sub VirgulaPunct()
Dim oRow As Range
Dim cell As Range
Dim i As Long, j As Long
Dim MyString As String
Dim aux As String

    Application.ScreenUpdating = False

    For i = Selection(Selection.Count).Row To Selection.Cells(1, 1).Row Step -1

        For j = Selection(Selection.Count).Column To Selection.Cells(1, 1).Column Step -1
            MyString = Cells(i, j).Value
            MyString = Replace(MyString, ",", ";+;", 1)
            MyString = Replace(MyString, ".", ",", 1)
            MyString = Replace(MyString, ";+;", ".", 1)
            Cells(i, j).Value = MyString
        Next j
    Next i
    Application.ScreenUpdating = True
End Sub

So as I said in my previous answer, I do 3 calls to Replace , but I do them for the whole string rather than per character in the string.

For future reference, it would probably be better if you updated your original question rather than to create a new one and then you could leave a comment for me under my answer and I'd seen that you have done so.

here's a simple function that cuts your string at "," in 2 pieces, then sticks them together with a dot instead. But It only works with one comma.

Public Function change_comma_to_dot(your_string As String)

change_comma_to_dot = Left(your_string, InStr(your_string, ",") - 1) & "." & Mid(your_string, InStr(your_string, ",") + 1)

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM