2

我在 32 位 XP 机器上的 Excel 2007 中创建了一个 vba 程序,该程序可以正常运行。但是,当我尝试在 64 位 Server 2008 R2 远程桌面服务机器上运行相同的电子表格时,我收到一个不可恢复的错误。这是导致问题的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ErrHandle

    Dim MachPhone As String
    Dim ConPhone As String

    Select Case Target.Row

    Case 12
        MachPhone = PhoneFormat(Range("B12"))
        Range("B12").Value = MachPhone
    Case 13
        ConPhone = PhoneFormat(Range("B13"))
        Range("B13").Value = ConPhone
    End Select

    ErrHandle:
        Exit Sub
        Resume

End Sub

PhoneFormat 函数检查电话号码的格式,如果无法确定电话号码是什么,则返回格式化的电话号码或原始字符串。

4

1 回答 1

3

禁用事件:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim val As String
Dim Phone As String
Dim rw As Long

    On Error GoTo ErrHandle

    rw = Target.Row
    If rw = 12 Or rw = 13 Then
        With Target.Parent.Cells(rw, 2)
            val = Trim(.Value)
            Phone = Phoneformat(val)
            If val <> Phone Then
                Application.EnableEvents = False
                .Value = Phone
                Application.EnableEvents = True
            End If
        End With
    End If
    Exit Sub

ErrHandle:
    Application.EnableEvents = True

End Sub
于 2011-07-07T20:25:37.653 回答