问题
If I type for example Sqr(a ^ 2 + b ^ 2)
there is no error. But when I type Sqr(a ^ 2 + b^ 2)
it produces a compile error which I do not understand:
![](https://www.eimg.top/images/2020/03/09/c1d2c9d78bb4ceb29b4e6e16f7a8ba44.png)
What is the function of ^
in VBA7 as opposed to _^
(underscore to show space) which denotes exponentiation?
回答1:
This is a 64 bit issue.
^ can confuse the compiler as to whether an operator versus operand value as LongLong is being used.
This problem occurs because the circumflex character (^) is ambiguous in this context. For 64-bit versions of VBA, the circumflex has two meanings:
- It can designate an exponent operation, such as x to the power of y ("x^y").
- For 64-bit Office 2010 VBA editions only, it can designate that an operand value should be treated as a LongLong, 64-bit integer value data type (for example, "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
来源:https://stackoverflow.com/questions/29035672/why-do-i-need-a-space-before-the-operator-in-vba-for-excel-2013-or-it-will-p