为什么 VBA 说它找到了一个“模棱两可的名字”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20255141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:19:06  来源:igfitidea点击:

Why is VBA saying that it has found an 'ambiguous name'?

vbaexcel-vbaexcel

提问by Kevin Pooley

When compiling some code (declarations shown below) I get the error message 'Compile Error: Ambiguous name detected. SixTables'. I have looked here and elsewhere but cannot find anything that matches my problem. What appear to be the most common causes of this error, declaring two variables with identical names or giving the same name to a function and the sub that it is called from, do not apply. And yes, I know I could just change the name to something the system was happy with, but (1) I wouldn't learn what I'm doing wrong, and (2) I chose that name for a reason - it fits its purpose exactly :-)

编译某些代码(如下所示的声明)时,我收到错误消息“编译错误:检测到名称不明确”。六桌'。我在这里和其他地方看过,但找不到任何符合我的问题的东西。似乎是此错误的最常见原因,即声明两个具有相同名称的变量或将相同名称赋予函数和调用它的子程序,并不适用。是的,我知道我可以将名称更改为系统满意的名称,但是 (1) 我不会知道我做错了什么,并且 (2) 我选择这个名称是有原因的 - 它适合它目的正是:-)

Option Explicit

Dim ArmOfService As Byte
Dim CharacterNumber As Long
Dim CurrentTerm As Byte
Dim DecorationRollMade As Byte
Dim DecorationRollNeeded As Byte
Dim DiceSize As Byte
Dim GenAssignment
Dim GenAssignmentSwitchInt As Byte
Dim GenAssignmentSwitchOff As Byte
Dim iLoopControl
Dim jLoopControl
Dim kLoopControl
Dim LineIncrement As Integer
Dim lLoopControl
Dim Merc(100)
Dim NoOfDice As Byte
Dim OfficerPromotion(63 To 78) As Byte
Dim PromotionRollMade As Byte
Dim PromotionRollNeeded As Byte
Dim Roll As Byte
Dim SkillColumn
Dim SixTables
Dim SpecAssignmentSwitchEnd As Byte
Dim SurvivalRollMade As Byte
Dim SurvivalRollNeeded As Byte
Dim TechLevel As Byte
Dim Temp As Integer
Dim Term As Byte
Dim TestCount
Dim UnitAssignment
Dim WhichTable
Dim Year As Byte

EDIT: I'm so embarrassed that I can hardly bring myself to explain what the problem was. I knew I hadn't duplicated a name, since I was only using it once - as a function! Thanks all for your help, I'm now going to go and hide my face in shame...

编辑:我很尴尬,我几乎无法解释问题是什么。我知道我没有重复名称,因为我只使用了一次 - 作为一个函数!谢谢大家的帮助,我现在要丢脸了……

采纳答案by Kairan

From MSDN :

从 MSDN :

  1. More than one object in the same scope may have elements with the same name.
  1. 同一范围内的多个对象可能具有相同名称的元素。

Module-level identifiers and project-level identifiers (module names and referenced project names) may be reused in a procedure, although it makes programs harder to maintain and debug. However, if you want to refer to both items in the same procedure, the item having wider scope must be qualified. For example, if MyID is declared at the module level of MyModule , and then a procedure-levelvariable is declared with the same name in the module, references to the module-level variable must be appropriately qualified:

模块级标识符和项目级标识符(模块名称和引用的项目名称)可以在一个过程中重复使用,尽管它使程序更难维护和调试。但是,如果您想在同一程序中引用这两个项目,则必须限定范围更广的项目。例如,如果 MyID 在 MyModule 的模块级别声明,然后在模块中使用相同名称声明过程级别变量,则必须适当限定对模块级别变量的引用:

Dim MyID As String 
Sub MySub 
MyModule.MyID = "This is module-level variable" 
Dim MyID As String 
MyID = "This is the procedure-level variable" 
Debug.Print MyID 
Debug.Print MyModule.MyID 
End Sub 
  1. An identifier declared at module-level conflicts with a procedure name.
  1. 在模块级声明的标识符与过程名称冲突。

For example, this error occurs if the variable MyID is declared at module level, and then a procedure is defined with the same name:

例如,如果在模块级别声明了变量 MyID,然后使用相同名称定义了一个过程,则会发生此错误:

Public MyID 
Sub MyID 
. . . 
End Sub 

回答by noviceProgrammer

Having had this issue many times, and not fully understanding why, I think there is an important fact that I have tested, and someone can confirm. It may be obvious to professional programmers, but I place this here for those that seek these answers on discussions, who are usually not professional programmers.

多次遇到这个问题,但不完全理解为什么,我认为有一个重要的事实,我已经测试过,有人可以确认。对于专业程序员来说,这可能是显而易见的,但我把它放在这里是为了那些在讨论中寻求这些答案的人,他们通常不是专业程序员。

A variable declared within a sub() is not "declared" (memory assigned) until that sub() is executed. And when the execution of that sub() is complete, the memory is released. Until now, I thought Public variables acted in similar way; available to any module that used it, --BUT only existing as long as the module where they were declared was still executing.

在 sub() 中声明的变量在执行 sub() 之前不会“声明”(分配内存)。当 sub() 执行完成时,内存被释放。直到现在,我认为公共变量的行为方式类似;可用于使用它的任何模块,--BUT 仅存在于声明它们的模块仍在执行时。

However, for any Public variable who's declaration line is in any module in the workbook, that variable is declared and available at the execution of any module within the workbook, even if the module where the Public declaration exists is never called or executed.

但是,对于声明行位于工作簿中任何模块中的任何 Public 变量,该变量在工作簿中的任何模块执行时都已声明并可用,即使从未调用或执行过 Public 声明所在的模块

Therefore, if you have a Public variable declaration in one module, and then again in a separate module that you plan to run independent of the first, Excel still sees 2 declarations for the same variable, and thus it is ambiguous.

因此,如果您在一个模块中有公共变量声明,然后又在您计划独立于第一个模块运行的单独模块中,Excel 仍会看到同一变量的 2 个声明,因此它是不明确的。

To prove it, you can create a blank module within a workbook project, and add absolutely nothing except two Public declaration lines,

为了证明这一点,您可以在工作簿项目中创建一个空白模块,并且除了两个 Public 声明行之外绝对不添加任何内容,



Public VariableX as String

公共变量X作为字符串

Public VariableY as Integer

公共变量 Y 作为整数



and those variables will be declared and available throughout the project, for any sub() executed. This fact is also probably the reason for the tip about minimizing the use of public variables.

并且这些变量将在整个项目中声明并可用,对于执行的任何 sub()。这个事实也可能是关于尽量减少使用公共变量的提示的原因。

Again, this may have been kindergarten level information for professional programmers, but most on this are not pros, and have to learn these lessons the hard way, or through discussion boards like this. I hope this helps someone.

同样,这对于专业程序员来说可能是幼儿园级别的信息,但大多数人都不是专业人士,必须通过艰苦的方式或通过像这样的讨论板来学习这些课程。我希望这可以帮助别人。