View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joeu2004[_2_] joeu2004[_2_] is offline
external usenet poster
 
Posts: 829
Default Optional arguments

"KD" wrote:
"Optional ByVal LB As Long = -1&, _"
What does the "&" after the -1 mean?


It is completely unnecessary in this context.

For every data type, there is a character that implicitly assigns a type to
a variable name or number when used as a prefix.

-1& says -1 should be treated as type Long. It is unnecessary here because
you are simply assigning -1 to a type Long variable.

It is also unnecessary if you specify a number larger than 32767, e.g.
33000. VBA knows to treat that as Long since it is larger than type
Integer.

The "&" suffix becomes important when the data type of an expression is
ambiguous. For example:

Dim n As Long
n = 30000 + 1000

results in an overflow error since 30000 and 1000 are both interpreted as
type Integer. Consequently, VBA tries to perform type Integer arithmetic
(addition). The following fixes the problem:

n = 30000& + 1000

In contrast, the following works without the use of "&":

n = 33000 + 1000

because 33000 is type Long; ergo, 1000 is "converted" to type Long, and type
Long arithmetic is performed.