VBA替换不使用数字格式

时间:2016-02-01 10:34:27

标签: string vba replace phone-number

我再一次坚持不懈。

我正在尝试清理电话号码数据,但代码没有做任何事情。

    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是手机号码所在的列。

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

你可以这样:

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