为什么 Excel 2013 VBA 中的“^”运算符之前需要一个 space 否则会产生编译时错误 "Expected list or separator"

Why do I need a space before the '^' operator in VBA for Excel 2013 or it will produce a compile time error "Expected list or separator"

如果我输入 Sqr(a ^ 2 + b ^ 2) 就没有错误。但是当我输入 Sqr(a ^ 2 + b^ 2) 时,它会产生一个我不理解的编译错误:

VBA7 中 ^ 的功能是什么,而不是 _^(下划线表示 space)表示求幂?

这是 64 位 issue.

^ 可能会混淆编译器是否正在使用运算符和操作数值,如 LongLong。

出现此问题是因为抑扬符 (^) 在此上下文中不明确。对于 64 位版本的 VBA,抑扬符有两个含义:

  1. 可以指定指数运算,比如x的y次方("x^y")。
  2. 仅对于 64 位 Office 2010 VBA 版本,它可以指定应将操作数值视为 LongLong、64 位整数值数据类型(例如,“234^”)。

Consider the following scenario:

You have the 64-bit edition of Microsoft Office 2010 installed on your computer. In the Visual Basic IDE, you create a new project.

In your Visual Basic for Applications (VBA) code, you type a statement that resembles the following:

LongLongVar2 = LongLongVar1^IntegerVar

When you type x^y in the 64-bit Office 2010 VBA editions, the VBA IDE editor does not know how to interpret the "x^" part of the string. In this context, "x^" can be treated as being the LongLong data type. If "x^" is interpreted in this manner, the string will produce an error because there is no operator that is specified for the "y" value to apply to the "x" value. If you type the string by using a space between the value name and the circumflex (for example, x ^y), you indicate that you intend the symbol to be an operator and not a data type designator. By doing this, you can avoid the error condition. Therefore, the example expression ... should be rewritten as follows:

LongLongVar2 = LongLongVar1 ^ IntegerVar