烦人的 vba 命名行为

发布于 2024-11-04 16:26:07 字数 780 浏览 0 评论 0原文

我使用的是 access 2007,可以按如下方式复制此行为。

1) 创建新的access数据库accdb文件。
2)打开数据库并创建新的vba模块。
3) 创建第一个子程序 sub1:

Sub sub1()
    Msgbox Err.Description
End Sub

4) 创建第二个子程序 sub2:

Sub sub2(Description as String)
    Msgbox Description
End Sub

此时一切正常。
5)但是,如果我去更改 sub2,使“Description”变为“description”,即将“D”更改为“d”,如下所示:

Sub sub2(description as String)
    Msgbox description
End Sub

这也会产生连锁反应,并且也会更改 sub1!所以 sub1 现在显示为:

Sub sub1()
    Msgbox Err.description
End Sub

为什么 'Err.Description' 更改为 'Err.description' ?

这种行为似乎对代码的实际功能没有影响,所以没有问题。我遇到的最大问题是我将 vba 模块导出为文本文件并将它们置于 SVN 控制之下。就在最近,大量毫无意义的“更改”已被提交到存储库中。

关于如何阻止这种情况发生有什么想法吗?

I'm using access 2007 and this behaviour can be replicated as follows.

1) Create new access database accdb file.
2) Open database and create new vba module.
3) Create 1st subroutine sub1:

Sub sub1()
    Msgbox Err.Description
End Sub

4) Create 2nd subroutine sub2:

Sub sub2(Description as String)
    Msgbox Description
End Sub

At this point everything is normal.
5) But if I go and change sub2 so that 'Description' reads 'description', i.e. change 'D' to 'd' like so:

Sub sub2(description as String)
    Msgbox description
End Sub

This also has a knock-on effect and changes sub1 too! So that sub1 now reads:

Sub sub1()
    Msgbox Err.description
End Sub

Why has 'Err.Description' changed to 'Err.description' ?

This behaviour seems to have no effect on the actual functionality of the code, so no problem there. The big issue I have is that I'm exporting my vba modules as text files and putting them under SVN control. And just recently a whole load of pointless 'changes' have been committed to the repository because of this.

Any ideas on how to stop this from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-11-11 16:26:07

对不起。这是 VBA 的硬编码“功能”。请参阅此处类似的问题:如何将默认情况恢复为 VBA (Excel 2010) 中的变量?

我使用源代码控制解决此问题的方法是通过执行以下操作的脚本运行我的存储库:

  1. 恢复所有修改带有 vba 代码扩展名的文件(创建备份 .orig 文件)
  2. 对 .orig 文件与其对应文件进行不区分大小写的比较
  3. 如果没有更改(除了大小写更改),请删除 .orig 文件
  4. 对于其余 .orig 文件(具有实际更改的文件)删除对应文件并删除 .orig 扩展名

这样做的作用是有效隐藏仅更改大小写的文件(正如您遇到的那样,在使用 VBA 文件时这是一个持续存在的问题)。它不会隐藏已对其进行其他修改的文件中的大小写更改。它远非完美的解决方案,但它是我想出的最好的解决方案。

Sorry. That is a hard-coded "feature" of VBA. See similar question here: How does one restore default case to a variable in VBA (Excel 2010)?

The way I've worked around that with source control is to run my repository through a script that does the following:

  1. Revert all modified files with vba code extensions (creating backup .orig files)
  2. Do a case-insensitive compare of the .orig files to their counterparts
  3. If there are no changes (outside of case changes) delete the .orig file
  4. For the remaining .orig files (the ones with actual changes) delete the counterpart file and remove the .orig extension

What this does is effectively hide files where the only changes are to the case (a constant problem when working with VBA files, as you're experiencing). It does not hide case changes in a file that has had other modifications done to it. It's far from a perfect solution but it's the best I've come up with.

枫林﹌晚霞¤ 2024-11-11 16:26:07

另外请记住,在 VBA 中,变量名不区分大小写。所以Description和description在同一范围内是相同的。

Additionally remember that in VBA, variable names are not case sensitive. So Description and description are the same within the same scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文