VBA 中不存在 += 运算符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17256153/
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-11 21:49:34  来源:igfitidea点击:

Does the += operator just not exist in VBA?

excelvbaexcel-vbaoperators

提问by StolenKitten

I'm trying to incriment the value in a cell, but despite documentation saying Visual Basic allows the += operator, it's just giving me "Compile error: Expected: expression".

我试图增加单元格中的值,但尽管文档说 Visual Basic 允许 += 运算符,它只是给我“编译错误:预期:表达式”。

Range("CellName").Value += 1

Is what's breaking, but if I do

是什么打破了,但如果我这样做

Range("CellName") = Range("CellName") + 1

It works fine

它工作正常

回答by Wild138

No, it doesn't exist in VBA.

不,它在 VBA 中不存在。

VB.NET might take +=(though I'm not even sure about that).

VB.NET 可能需要+=(虽然我什至不确定)。

You'll have to use

你必须使用

Range("CellName").Value = Range("CellName").Value+1

A good reference can be found here

一个很好的参考可以在这里找到

回答by hkwint

An "official" list of VBA-operators can be found in VBA help here:

可以在此处的 VBA 帮助中找到 VBA 运算符的“官方”列表:

Help --> Microsoft Visual Basic Help --> Visual Basic for Applications Language Reference --> Visual Basic Language Reference --> Operators --> Arithmetic Operators (online here)

帮助 --> Microsoft Visual Basic 帮助 --> Visual Basic for Applications 语言参考 --> Visual Basic 语言参考 --> 运算符 --> 算术运算符(此处在线

Maybe controversially, but the next advice would have saved me lots of time and headaches:

也许有争议,但下一个建议会为我节省大量时间和头痛:

I'd say it's better to forget everythingabout VB and focus at VBA, as the two are different languages. People don't mention "Oh, OCaml, APL, PHP or Fortran have increment operators", as it's off-topic if the scope is VBA. In the same way, what VB has or has not is off-topic, and usually it only adds to the confusion because it's resemblance. Better use the MS-Office provided "local" Visual Basic for Applications Language Reference as provided by the help system, or online:

我会说最好忘记有关 VB 的所有内容并专注于 VBA,因为两者是不同的语言。人们不会提到“哦,OCaml、APL、PHP 或 Fortran 有增量运算符”,因为如果范围是 VBA,它就离题了。同样,VB 有或没有什么是题外话,通常它只会增加混乱,因为它是相似的。更好地使用 MS-Office 提供的“本地”Visual Basic for Applications Language Reference,由帮助系统或在线提供:

http://msdn.microsoft.com/en-us/library/office/gg264383%28v=office.15%29.aspx

http://msdn.microsoft.com/en-us/library/office/gg264383%28v=office.15%29.aspx

回答by Behrooz Karjoo

It's really annoying. I made the following functions to make my life a bit easier:

这真的很烦人。我做了以下功能,让我的生活更轻松:

' ++
Function pp(ByRef x, Optional y = 1)
    x = x + y
    pp = x
End Function

' --
Function mm(ByRef x, Optional y = 1)
    x = x - y
    mm = x
End Function

Doesn't really help your situation as you'd still have to type the same amount:

并没有真正帮助您的情况,因为您仍然必须输入相同的数量:

Range("CellName") = pp(Range("CellName"))

but for simple loop increments it helps :

但对于简单的循环增量它有帮助:

do while cells(x,1) <> ""
    pp x
loop