gpt4 book ai didi

excel - Erl() 函数在 64 位 O365 Excel VBA 中不起作用

转载 作者:行者123 更新时间:2023-12-04 20:50:51 25 4
gpt4 key购买 nike

我使用使用 vba Erl() 函数进行错误处理的 Excel 电子表格。 vba 代码可能如下所示:

   Sub doSomething()
Dim v as variant
Dim v1 as Variant

On Error goto err_Handler

10 v = "Test"

20 v1 = 1/0 ' Generate error

30 Call doMoreStuff()

Exit Sub

err_Handler:

if Erl = 20 then
Resume Next ' Ignore this error

Else
Msgbox "Looks Like there is a REAL error somewhere. Desc = " & Err.Description
End If

End Sub

我不会以这种方式使用 Erl 做出判断。就个人而言,我从不这样做,但我们有很多电子表格都有这个。
但是,在 64 位 Excel 上的 O365 测试中,此功能似乎根本不起作用。在上面的代码中,程序没有正确读取行号,因此行 Erl = 20 在应该为 True 时返回 False,因此程序在应该忽略错误时调用 Msgbox。
有没有其他人注意到这一点?显然有几十个电子表格存在这个潜在问题。创建一个以某种方式将代码更改为其他内容的脚本将是一项艰巨的任务。
有没有其他人不得不处理这个问题,你能概述一下你是如何解决这个问题的吗?
非常感谢

最佳答案

Has anyone else noticed this?


是的,我可以确认这个问题也存在于 64 位版本的 MS Access 中,当将数据库编译为 accde 时或 mde格式。这是我的复制说明(使用版本 2112、16.0.14729.20260 C2R 测试)。
  • 使用单个模块创建一个新的空数据库。
  • 使用以下代码填充模块:
    Sub Test()
    Dim a As Long
    Dim error_lines As String

    On Error GoTo error_handler

    10: a = 1 / 0
    20: a = 1 / 0
    30: a = 1 / 0
    40: a = 1 / 0
    50: a = 1 / 0

    Debug.Print error_lines
    Exit Sub

    error_handler:
    error_lines = error_lines & Erl() & ","
    Resume Next

    End Sub
  • 执行代码并注意它打印出 10,20,30,40,50,在即时窗口中。到目前为止,一切都很好。
  • 创建编译accde文件与文件/另存为...
  • 打开accde文件,按Ctrl-G进入VBA即时窗口,执行Test .
  • 请注意,现在的输出是 10,20,0,0,0,

  • Has anyone else had to deal with this problem and can you outline how you addressed this problem?


    显而易见的解决方法是使用您自己的错误行变量,即替换
    10: FrobnicateFoo()
    20: ...

    error_handler:
    If Erl() = 10 ...
        Dim line_nr As Long

    line_nr = 10
    FrobnicateFoo()
    line_nr = 20
    ...

    error_handler:
    If line_nr = 10 ...
    但是,这将是一个用命名良好的变量替换那些无意义的行号的好机会,例如
        Dim frobnicating_foo As Boolean

    frobnicating_foo = True
    FrobnicateFoo()
    frobnicating_foo = False
    ...

    error_handler:
    If frobnicating_foo Then ...

    关于excel - Erl() 函数在 64 位 O365 Excel VBA 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62942335/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com