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
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
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:
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
No it does not have it for numbers, e.g. see http://msdn.microsoft.com/de-de/library/215yacb6%28v=vs.80%29.aspx / http://msdn.microsoft.com/en-us/library/cey92b0t.aspx