View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
JE McGimpsey JE McGimpsey is offline
external usenet poster
 
Posts: 4,624
Default USING THE "&" IN VBA FORMULAS !

Breaking

(1) Range("A1").Formula = "=SUM(" & rMyRange.Address(0, 0) & ")"

down, the right-hand argument of the assignment operator ("=") is
evaluated before assignment to the .Formula property of the range
Range("A1"). "&" operates on strings only (non-string arguments need to
be coerced, either explicitly - e.g., CStr(x) - or implicitly). Since
"=SUM(" and ")" are elemental strings, but

rMyRange.Address(0, 0)

is not, the latter needs to be evaluated first. Evaluation of the
address property returns the string

(2) "J1:K100"

The first "&" concatenates the string "=SUM(" and (2) to return the
string:

(3) "=SUM(J1:K100"

The second "&" concatenates the result in (3) with ")" to return the
string:

(4) "=SUM(J1:K100)"

So after both concatenations, the assignment in (1) is equivalent to:

(5) Range("A1").Formula = "=SUM(J1:K100)"


In article ,
jay dean wrote:

Question1:
From Range("A1").Formula = "=SUM(" & rMyRange.Address(0, 0) & ")".
Suppose rMyrange was initially set to J1:K100 as you say.
So what exactly is the first ampersand (&) in the formula
concatenating?
What exactly is the second '&' concatenating?
These are what confuse me regarding the use of the ampersand in
formulas. Please break it (the explanation) down for me.