excel vba取代失败

时间:2015-11-19 21:59:25

标签: excel-vba excel-2010 vba excel

直到最近,我一直运行以下宏几年没问题。

过去几周,我必须手动更换到合适的纸张才能使用。今天它停止这样做了,所以我逐步完成了它。看到它是在第一个替换语句之后退出,是否有要替换的数据。

Sub Clean_Phone()
'
' Clean_Phone Macro
'
' Last Update - 5 Feb 2015
'
    Dim tSHeet As String
    Dim r As Range
    On Error Resume Next    ' restore Find/Replace settings to default
    Set r = Cells.Find(What:=vbNullString, LookIn:=xlFormulas, _
                       SearchOrder:=xlRows, LookAt:=xlPart, MatchCase:=False)
    '    On Error GoTo 0
    tSHeet = ActiveSheet.Name

    Sheets("Data").Select  ' DataTbl is 15 col x > 1100 row
    With Sheets("Data").Range("DataTbl[[Latitude]:[Longitude]]")
        .Replace What:="°", Replacement:=vbNullString, LookAt:=xlPart
    End With

    Sheets("Data").Select
    With Sheets("Data").Range("DataTbl[[Phone]:[Phone2]]")  ' DataTbl is 15 col x >1100 row
        .Replace What:=" ", Replacement:=vbNullString, LookAt:=xlPart
        .Replace What:=")", Replacement:=vbNullString, LookAt:=xlPart
        .Replace What:="-", Replacement:=vbNullString, LookAt:=xlPart
        .Replace What:="(", Replacement:=vbNullString, LookAt:=xlPart
        .Replace What:=".", Replacement:=vbNullString, LookAt:=xlPart
    End With

    Range("DataTbl[[Phone]:[Phone2]]").NumberFormat = "[<=9999999]###-####;(###) ###-####"

    With Sheets("Data").Range("DataTbl[Address]")
        .Replace What:=" nw ", Replacement:=" NW ", LookAt:=xlPart
        .Replace What:=" ne ", Replacement:=" NE ", LookAt:=xlPart
        .Replace What:=" se ", Replacement:=" SE ", LookAt:=xlPart
        .Replace What:=" sw ", Replacement:=" SW ", LookAt:=xlPart
    End With

如果On Error GoTo 0被注释掉,似乎没有任何区别,坦率地说,我还没有找到Set r = Cells.Find( ...语句的用途。

我不是百分百肯定,但我认为在升级到Win 10后,该功能正常运行了一段时间。

2 个答案:

答案 0 :(得分:1)

我不能说我有你的答案,但我可以回答几点并给你一点修改,试试。

你应该保持On Error Resume Next和On Error Goto 0,因为它们是一个故障捕获围绕该语句你不知道它为什么存在。正如它所说,它正在重置查找/替换默认值。我不知道这里是否需要它们,或者是否因为搜索空单元格而需要故障捕获,但将它保留在那里并没有坏处,但重置错误处理程序非常重要。

我会删除选择数据表的语句,这是不必要的,并且更明智地使用Withs。

这是修改后的代码,值得在我的视图中尝试

Sub Clean_Phone()
'
' Clean_Phone Macro
'
' Last Update - 5 Feb 2015
'
Dim tSHeet As String
Dim r As Range

    On Error Resume Next
    ' restore Find/Replace settings to default
    Set r = Cells.Find(What:=vbNullString, LookIn:=xlFormulas, _
                   SearchOrder:=xlRows, LookAt:=xlPart, MatchCase:=False)
    On Error GoTo 0
    tSHeet = ActiveSheet.Name

    With Sheets("Data")

        With .Range("DataTbl[[Latitude]:[Longitude]]")
            .Replace What:="°", Replacement:=vbNullString, LookAt:=xlPart
        End With

        With .Range("DataTbl[[Phone]:[Phone2]]")  ' DataTbl is 15 col x >1100 row
            .Replace What:=" ", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:=")", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="-", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:="(", Replacement:=vbNullString, LookAt:=xlPart
            .Replace What:=".", Replacement:=vbNullString, LookAt:=xlPart

            .NumberFormat = "[<=9999999]###-####;(###) ###-####"
        End With

        With .Range("DataTbl[Address]")
            .Replace What:=" nw ", Replacement:=" NW ", LookAt:=xlPart
            .Replace What:=" ne ", Replacement:=" NE ", LookAt:=xlPart
            .Replace What:=" se ", Replacement:=" SE ", LookAt:=xlPart
            .Replace What:=" sw ", Replacement:=" SW ", LookAt:=xlPart
        End With
    End With
End Sub

答案 1 :(得分:0)

我发现了问题。

我有一个完全不同的工作簿,它干扰了这个。我不知道怎么样 - 他们没有共享任何模块,但是当我关闭它时,这个模块开始表现正确。

感谢大家的努力!