在Excel脚本中用点替换逗号

时间:2011-02-03 09:44:03

标签: excel vba excel-vba

我想用点替换逗号,反之亦然

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

6 个答案:

答案 0 :(得分:2)

您可以使用Range对象的Replace方法

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

答案 1 :(得分:2)

基本问题是,如果将列设置为通用而不是文本,即使我们更改了"," for"。" Excell会自动为#34;,#34;再次更改它。

为了避免首先将列设置为Text格式,然后我们可以执行replace函数:

这对我有用:

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

答案 2 :(得分:0)

通过查看循环,您似乎不会多次访问任何单元格。一个简单的if语句不会在这里工作吗?

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

答案 3 :(得分:0)

从VB的角度而不是VBA来看,我会考虑使用内置的Replace函数而不是循环它。

首先,我将所有,替换为;(或者如果文本中可能有;,我会使用其他内容,可能是字符的组合,例如{ {1}}或类似的),然后将所有;+;+替换为.,最后将所有,替换为;
可能效率不高,但很容易。

答案 4 :(得分:0)

这似乎是我对你上一个问题的答案的延续,但如果是这样,我认为你误解了我的意思。我已经接受了你的代码,并根据我的建议对其进行了修改,但我没有对其进行测试:

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

正如我在之前的回答中所说,我对Replace进行了3次调用,但是我对整个字符串执行了调用,而不是字符串中的每个字符。

为了将来参考,如果您更新原始问题而不是创建新问题可能会更好,然后您可以根据我的回答给我发表评论并且我已经看到您已经这样做了。

答案 5 :(得分:0)

这是一个简单的函数,将您的字符串切成两段,然后将其与点连在一起。但是它只能用一个逗号。

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