Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA shorthand for x=x+1?

Tags:

vba

ms-access

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

like image 548
gregg Avatar asked Nov 19 '13 15:11

gregg


People also ask

What does X mean in VBA?

DIM in VBA can be termed as it declares variable in different data types such as integer Boolean string or double etc. In any programming language, a variable needs to be declared to a specific data type such as X is a variable and if we define X as an integer which means we can store integer values in X.

Is there a += in VBA?

According to that official list, VBA doesn't have a += operator.

How do you increment a variable in excel VBA?

You can increment counter variable by any value you like. The Step keyword tells excel to increment counter variable by the specified value. The Step 2 code tells excel to increment i each time through the loop by 2.

What does #if mean in VBA?

The Microsoft Excel IF-THEN-ELSE statement can only be used in VBA code. It executes one set of code if a specified condition evaluates to TRUE, or another set of code if it evaluates to FALSE. The IF-THEN-ELSE statement is a built-in function in Excel that is categorized as a Logical Function.


2 Answers

Sadly there are no operation-assignment operators in VBA.

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

Pointless workaround;

Sub Inc(ByRef i As Integer)    i = i + 1   End Sub ... Static value As Integer inc value inc value 
like image 55
Alex K. Avatar answered Sep 19 '22 07:09

Alex K.


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.

like image 45
etfa Avatar answered Sep 21 '22 07:09

etfa