【问题标题】:VBA Replace not working with numbers formatVBA替换不适用于数字格式
【发布时间】:2016-02-01 10:34:27
【问题描述】:

我又一次陷入了困境。

我正在尝试清理电话号码数据,但代码没有执行任何操作。

    Columns(icount).Replace What:=",", Replacement:="", LookAt:=xlWhole, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False


    Columns(icount).Replace What:="-", Replacement:="", LookAt:=xlWhole, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

icount 是电话号码所在的列。

我不明白为什么它不起作用。用“à”替换“à”可以正常工作。

【问题讨论】:

    标签: string vba replace phone-number


    【解决方案1】:

    尝试使用“LookAt:=xlPart”而不是“LookAt:=xlWhole”

    Columns(icount).Replace What:=",", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    
    
    Columns(icount).Replace What:="-", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    

    【讨论】:

      【解决方案2】:

      你可以这样:

      Sub rep()
      
      
      For Each c In Sheets("Sheet1").Range("A:A").Cells ' Change the range that you want
      
          If InStr(c.Value, ",") > 0 Then
              deli = Split(c, "")
              For a = 0 To UBound(deli)
              c.Value = replace(c.Value, ",", "")
              Next a
      
          End If
      
          If InStr(c.Value, "-") > 0 Then
              deli = Split(c, "")
              For a = 0 To UBound(deli)
              c.Value = replace(c.Value, "-", "")
              Next a
      
          End If
      
      Next c
      
      End Sub
      

      【讨论】:

      • 嗨,这正是我想要的。但我真的不明白。
      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 2018-11-29
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多