Shorthand for x=x+1?

2019-02-16 03:51发布

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 Explicit with no change

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

2条回答
老娘就宠你
2楼-- · 2019-02-16 04:40

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.

查看更多
倾城 Initia
3楼-- · 2019-02-16 04:53

Sadly there are no operation-assignment operators in VBA.

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

Pointless workaround;

function Inc(ByRef i As Integer)
   i = i + 1  
end function
...
Static value As Integer
inc value
inc value
查看更多
登录 后发表回答