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”

我的梦境 提交于 2019-12-02 13:58:52

问题


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:

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:

  1. It can designate an exponent operation, such as x to the power of y ("x^y").
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!