所以这是错误:在 64 位 VBA 主机(例如 Access 365 64 位或 Excel 2016 64 位)创建类模块 SomeClass
:
' this needs to be here to trigger the bug:
Private Sub Class_Terminate()
End Sub
然后是一些模块Test
:Function ReturnFalse(o As Object) As Boolean
ReturnFalse = False
End Function
Sub Test()
Debug.Print ReturnFalse(New SomeClass)
If ReturnFalse(New SomeClass) Then
Debug.Print True
Else
Debug.Print False
End If
End Sub
现在,如果您使用 32 位 VBA 主机并在即时窗口中运行“测试”,出现预期的结果:False
False
但是,如果您使用的是 64 位 VBA主机,然后就出来了:False
True
除非,当您删除或重命名 Class_Terminate()
sub,在这种情况下会出现正确的输出。我已经将错误跟踪到这个最小的示例。显然,问题似乎在于,使用临时对象(此处为
new SomeClass
)会破坏对 IF
的评估。不知何故,使条件的值看起来是 True
没关系。好的,这是一个严重的错误,因为 64 位编译器是疯狂的,所有
IF
有麻烦了。全部
IF
? WHILE
呢? ?While ReturnFalse(New SomeClass)
Debug.Print "Oh no!"
Wend
是的,WHILE
也有麻烦,因为这会打印“哦,不!”在一个循环中。这很麻烦,我可以在任何地方重现它:
..还有 Excel 当然。
摘要:我可以在我拥有的所有版本的 Office 中找到这个错误,从 2013 年开始,它可能至少有 8 年的历史。
好的,这个错误之前是否影响过其他人?是的:
去年的这个帖子:
VBA takes wrong branch at If-statement - severe compiler bug?
2018 年 10 月在 excel.uservoice.com(显然是微软的用户意见箱之类的)中的这篇帖子:
https://excel.uservoice.com/forums/304921-excel-for-windows-desktop-application/suggestions/35735881-fix-inlined-member-calls-on-user-objects-on-64-bi
好吧,让我们提交一个错误报告。
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_mac-mso_mac2016/how-do-i-report-vba-bugs/bb4e5dea-9996-4232-9b5b-7dd57f76736c
If, after testing with others, the code fails and it really shouldn't, you can report the issue directly to Microsoft using the Smile button in Excel.
什么?
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_win10-mso_2016/excel-2016-vba-bug/b799dfc2-7cef-417d-8a41-96661a360c43
- Open Excel > File > Feedback > Send a Frown
- Through Uservoice - Click the following link to see the feedback of others and to provide feedback - https://excel.uservoice.com/
这不是对新图标配色方案的建议。这是一个已有 8 年历史的错误,它使 Access 应用程序和带有宏的 Excel 工作表计算出错误的答案(并且它还阻止了此处向 Office 64 的迁移,因为我们无法获取我们的代码)。
现在这是我的问题:
更新 : x-发布到
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-msoffice_custom-mso_2019/invalid-code-by-vba-64-bit-compiler/b91f984a-194c-4453-b8c5-02881afaf83b
更新 2 :
我有机会在 Office 365 for Mac 安装(其中 Win64 定义为
true
)上尝试代码,并且错误没有出现在那里。所以现在是PC的事情。更新 3 :
邮寄到 HN 和 The Register:
https://www.theregister.com/2021/08/19/64_bit_microsoft_vba_bug/
https://news.ycombinator.com/item?id=28188251
更新 4 :
今天(2021 年 11 月 15 日)刚刚检查了 Office 365 和 错误已消失 !看来有人关注了。但是,我无法弄清楚,我今年收到的大量累积更新中的哪一个起到了作用,但还不知道其他 Office 版本是否也已修复。
最佳答案
Sub Test()
Debug.Print ReturnFalse(New SomeClass)
If ReturnFalse(New SomeClass) Then
Debug.Print True
Else
Debug.Print False
End If
If True = ReturnFalse(New SomeClass) Then
Debug.Print True
Else
Debug.Print False
End If
End Sub
退货False
True
False
所以If True = ReturnFalse(New SomeClass) Then
修复它对于循环,这也修复了它
Do While True = ReturnFalse(New SomeClass)
Debug.Print "Oh no!"
Exit Do
Loop
强烈推荐评论每使用上面的解决方法,所以没有人删除
True =
将来(例如,因为他以 32 位开发,甚至没有遇到问题)。' Don't remove `True =` due to a compiler bug in x64 the condition would always be true.
' See https://stackoverflow.com/questions/68034271/how-can-i-get-this-8-year-old-vba-64-bit-compiler-bug-fixed
If True = ReturnFalse(New SomeClass) Then
偶If ReturnFalse(New SomeClass) And False = True Then
将是 True
有了这个错误。
关于excel - 如何修复这个有 8 年历史的 VBA 64 位编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68034271/