我想用点替换逗号,反之亦然
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
Run Code Online (Sandbox Code Playgroud)
小智 6
基本问题是,如果将列设置为常规而不是文本,即使我们将“,”更改为“。” Excell 会自动将其再次更改为“,”。
为了避免这种情况,需要先将列设置为文本格式,然后我们可以执行替换功能:
这对我有用:
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
Run Code Online (Sandbox Code Playgroud)
这似乎是我对你之前问题的回答的延续,但如果是这样,我认为你误解了我的意思。我已经采用了您的代码并根据我的建议对其进行了修改,但我还没有对其进行测试:
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
Run Code Online (Sandbox Code Playgroud)
正如我在之前的回答中所说,我对 进行了 3 次调用Replace
,但我是针对整个字符串而不是字符串中的每个字符进行调用。
为了将来的参考,如果您更新原来的问题而不是创建一个新问题,那么您可以在我的答案下为我留下评论,我发现您已经这样做了,这可能会更好。
归档时间: |
|
查看次数: |
55796 次 |
最近记录: |