Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the += operator just not exist in VBA?

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".

Range("CellName").Value += 1

Is what's breaking, but if I do

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

It works fine

like image 722
StolenKitten Avatar asked Jun 22 '13 23:06

StolenKitten


People also ask

Does != Work in VBA?

Bookmark this question. Show activity on this post. The problem is that != does not work as a function in excel vba.

What does <> signify in VBA?

The <> operator means c. Address Is Not Equal To firstAddress . In a C-style language this would be equivalent to c. Address !=

How do you use the OR operator in Excel VBA?

VBA If OR Operator “If (1 = 1) Or (5 = 0) Then” the if statement uses the OR logical operator to combine two conditions (1 = 1) And (5 = 0). If any of the conditions is true, the code above Else keyword is executed. If both conditions are false, the code below Else keyword is executed.


Video Answer


2 Answers

No, it doesn't exist in VBA.

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

You'll have to use

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

A good reference can be found here

like image 196
Wild138 Avatar answered Oct 16 '22 05:10

Wild138


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

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

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

I'd say it's better to forget everything about 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:

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

like image 35
hkwint Avatar answered Oct 16 '22 05:10

hkwint