Jon*_*tus 149 vba variable-assignment
希望这是一个简单的问题,但我非常喜欢这个技术答案!
有什么区别:
i = 4
Run Code Online (Sandbox Code Playgroud)
和
Set i = 4
Run Code Online (Sandbox Code Playgroud)
在VBA?我知道后者会抛出错误,但我不完全理解为什么.
Tre*_*reb 95
set用于指定对象的引用.C等价物是
int i;
int* ref_i;
i = 4; // Assigning a value (in VBA: i = 4)
ref_i = &i; //assigning a reference (in VBA: set ref_i = i)
Run Code Online (Sandbox Code Playgroud)
Tom*_*lak 77
在您的情况下,它会产生错误.:-)
Set分配对象引用.对于所有其他赋值(隐式,可选和很少使用)Let语句是正确的:
Set object = New SomeObject
Set object = FunctionReturningAnObjectRef(SomeArgument)
Let i = 0
Let i = FunctionReturningAValue(SomeArgument)
' or, more commonly '
i = 0
i = FunctionReturningAValue(SomeArgument)
Run Code Online (Sandbox Code Playgroud)
Gal*_*ian 43
来自MSDN:
设置关键字: 在VBA中,必须使用Set关键字来区分对象的赋值和对象的默认属性的赋值.由于Visual Basic .NET不支持默认属性,因此不再需要Set关键字,也不再支持.
| 归档时间: |
|
| 查看次数: |
197207 次 |
| 最近记录: |