【问题标题】:Why doesn't my VBA code work when the selection starts with a dot?为什么在选择以点开始时,我的VBA代码不起作用?
【发布时间】:2021-10-05 20:07:32
【问题描述】:

我创建了这段代码,它应该用点替换选择中的逗号(如果有的话)。但是,如果选择以包含点的单元格开头,则该代码不起作用,但如果它以包含逗号的单元格开头,则该代码有效。如果它以逗号开头,然后是带有点的单元格,然后是带有逗号的单元格,它甚至可以工作。代码如下:

Public Sub DeleteDotsReplaceCommasWithDots()
        
For Each cell In Selection

    Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    If InStr(ActiveCell.Value, ".") > 0 And InStr(ActiveCell.Value, ",") > 0 Then
        Selection.Replace What:=".", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    ElseIf InStr(ActiveCell.Value, ".") = 0 And InStr(ActiveCell.Value, ",") > 0 Then
        Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormul
    End If
    
Next cell

End Sub

知道为什么会这样吗?谢谢:)

【问题讨论】:

  • 欢迎来到 SO!我的印象是您循环遍历所有选定的单元格,但是,在每个循环中,您的条件都基于活动单元格,并且您在整个选择中进行替换。另外,我认为最后一个Replace 函数中的xlReplaceFormul 是一个错字...
  • 嗨维克多,感谢您的回答。我修正了错字。我还用“ActiveCell.Replace”更改了“Selection.Replace”,但是当一个带有点的单元格是第一个单元格时,我仍然无法让代码工作。
  • ActiveCell 不会解决问题,因为它不会随着For Each cell In Selection 循环的每次迭代而改变。也许切换到cell
  • 所以我错误地理解了选择中的每个单元格,我的错!但是谢谢你,当我把它改成单元格时,它工作得很好! :)

标签: excel vba replace


【解决方案1】:

您似乎是在 ActiveCell 上运行搜索/替换,而不是使用 For..Each 循环循环遍历的每个单元格。我已经使用 With cell.. End With 块稍微整理了一下。

试试这个:

Public Sub DeleteDotsReplaceCommasWithDots()
        
    For Each cell In Selection
        With cell
            .Replace What:=" ", Replacement:="", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
            If InStr(.Value, ".") > 0 And InStr(.Value, ",") > 0 Then
                .Replace What:=".", Replacement:="", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
                .Replace What:=",", Replacement:=".", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
            ElseIf InStr(.Value, ".") = 0 And InStr(.Value, ",") > 0 Then
                .Replace What:=",", Replacement:=".", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula
            End If
        End With
    Next cell

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2013-08-06
    • 1970-01-01
    • 2023-02-11
    相关资源
    最近更新 更多