VBA不断将句点更改为逗号

发布于 2025-01-13 06:54:27 字数 1507 浏览 3 评论 0原文

我制作了一个 vba 脚本,该脚本应该将特定区域的值从逗号分隔更改为点分隔。

这是代码:

Sub commas_to_periods()
'
' commas_to_periods Macro
' Convert all commas to Periods in a specified area that contains values before converting the file to csv
'

'
    Range("U3:AJ139").Select
    ' Selection.Replace What:=",", Replacement:=".", SearchOrder:=xlByColumns, MatchCase:=True
    ' Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
    '    SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat:=False, _
    '    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    
    Dim V As Variant, i As Long, j As Long, T
    V = Range("U3:AJ139").Value
    howMany = 0
    Application.ScreenUpdating = False
    T = Timer
    For i = LBound(V, 1) To UBound(V, 1)
        For j = LBound(V, 2) To UBound(V, 2)
            If InStr(V(i, j), ",") > 0 Then
                V(i, j) = Replace(V(i, j), ",", ".")
                howMany = howMany + 1
            End If
        Next j
    Next i
    Range("U3:AJ139").Value = V
    msgForBox = "That took " & Timer - T & " Seconds and found: " & howMany & " commas"
    MsgBox (msgForBox)
    Application.ScreenUpdating = True
    
End Sub

正如您所见,我尝试了几种方法,但我还使用了自定义循环来计算替换次数。我就是无法让它发挥作用!消息框显示它发现了 100 多个逗号,但宏完成后没有任何更改。下次运行它时,我会在框中收到完全相同的消息。发现相同数量的逗号 - 没有变化!

如果我尝试更换其他任何东西,它就可以正常工作。进行了替换。当我用逗号尝试时,它顽固地不起作用。现在我想这与我的区域设置有关,但我无法找到解决方法。当我进行手动搜索并替换逗号时,逗号会发生变化。我什至录制了一个进行手动搜索和替换的宏,并且得到了相同的结果。 VBA 似乎恢复了我的更改以符合我的区域设置。有办法解决这个问题吗?

I made a vba script that should change a specific area's values from comma-separated to dot-separated.

Here is the code:

Sub commas_to_periods()
'
' commas_to_periods Macro
' Convert all commas to Periods in a specified area that contains values before converting the file to csv
'

'
    Range("U3:AJ139").Select
    ' Selection.Replace What:=",", Replacement:=".", SearchOrder:=xlByColumns, MatchCase:=True
    ' Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
    '    SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat:=False, _
    '    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    
    Dim V As Variant, i As Long, j As Long, T
    V = Range("U3:AJ139").Value
    howMany = 0
    Application.ScreenUpdating = False
    T = Timer
    For i = LBound(V, 1) To UBound(V, 1)
        For j = LBound(V, 2) To UBound(V, 2)
            If InStr(V(i, j), ",") > 0 Then
                V(i, j) = Replace(V(i, j), ",", ".")
                howMany = howMany + 1
            End If
        Next j
    Next i
    Range("U3:AJ139").Value = V
    msgForBox = "That took " & Timer - T & " Seconds and found: " & howMany & " commas"
    MsgBox (msgForBox)
    Application.ScreenUpdating = True
    
End Sub

I tried a couple of ways as you see but I also used that custom loop to have it count the replacements. I just can't get it to work! The message box displays that it found 100+ commas, but when the macro is done no change is made. The next time I run it, I get the exact same message in the box. Same number of commas found - no change!

If I try to replace anything else, it works just fine. Replacements are made. When I try it with the comma, it stubbornly doesn't work. Now I suppose it has something to do with my regional settings but I can't figure out a workaround. When I do a manual search and replace the commas get changed. I even recorded a macro of me making a manual search and replace and I got the same result. It seems that VBA reverts my changes to comply with my regional settings. Is there a way to go around that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Spring初心 2025-01-20 06:54:27

请注意,如果您的区域设置设置为 , 作为小数点,则意味着任何包含 . 的数字都将被视为文本。

因此,如果您希望 Excel 不将它们转换为数字,则需要确保单元格的数字格式设置为文本:.NumberFormat = "@",然后再写入包含 的数字. 点。

例如

Sub a()
    
    Selection.NumberFormat = "@"
    Selection = Replace(Selection, ",", ".")

End Sub

,对于多个单元格,您将需要循环:

Sub b()   
    Dim Rng As Range
    Set Rng = Selection
    
    Rng.NumberFormat = "@"
    
    Dim Cell As Variant
    For Each Cell In Rng.Cells
        Cell.Value2 = Replace$(Cell.Value2, ",", ".")
    Next Cell
End Sub

因为任何其他解决方案都不起作用,因为 Excel 认为它很智能,并将任何文本重新转换为数字。

输入图片此处描述

Note that if your regional settings are set to , as decimal points that means any numeber that contains a . will be treated as Text.

So if you want that Excel does not convert them into a number you need to ensure the number format of your cell is set to text: .NumberFormat = "@" before writing the number containing a . dot.

For example

Sub a()
    
    Selection.NumberFormat = "@"
    Selection = Replace(Selection, ",", ".")

End Sub

For multiple cells you will need to loop:

Sub b()   
    Dim Rng As Range
    Set Rng = Selection
    
    Rng.NumberFormat = "@"
    
    Dim Cell As Variant
    For Each Cell In Rng.Cells
        Cell.Value2 = Replace$(Cell.Value2, ",", ".")
    Next Cell
End Sub

because any other solution does not work as Excel thinks it is smart and turns any text back into a number.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文