x=x+1 的 VBA 简写?

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

VBA shorthand for x=x+1?

vbams-accessaccess-vba

提问by gregg

Sub btn1_Click()
Static value As Integer
value = value + 1
MsgBox value
End Sub

I swear when I was taking a VB.net course in college there was a shorter way to tell a variable to add '' to itself. Maybe x=+1. I am using Access now though instead of visual studio. When I try that within the VBE it removes the +. I even removed Option Explicitwith no change

我发誓,当我在大学上 VB.net 课程时,有一种更短的方法可以告诉变量将 '' 添加到自身。也许 x=+1。我现在使用 Access 而不是 Visual Studio。当我在 VBE 中尝试时,它会删除 +。我什至Option Explicit没有改变就删除了

Assuming the answer will be no, there is no way to short-hand it & its just a peculiarly of VBA

假设答案是否定的,没有办法简写它,它只是 VBA 的一个特殊之处

回答by Alex K.

Sadly there are no operation-assignmentoperators in VBA.

遗憾的是operation-assignment,VBA中没有运算符。

(Addition-assignment +=et al are available in VB.Net)

(加法赋值+=等可在 VB.Net 中获得)

Pointless workaround;

无意义的解决方法;

function Inc(ByRef i As Integer)
   i = i + 1  
end function
...
Static value As Integer
inc value
inc value

回答by etfa

If you want to call the incremented number directly in a function, this solution works bettter:

如果你想直接在函数中调用递增的数字,这个解决方案效果更好:

Function inc(ByRef data As Integer)
    data = data + 1
    inc = data
End Function

for example:

例如:

Wb.Worksheets(mySheet).Cells(myRow, inc(myCol))

If the function inc()returns no value, the above line will generate an error.

如果函数inc()没有返回值,上面的行将产生一个错误。