Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion syntax
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Laurent Longre  
View profile  
 More options Dec 4 1998, 3:00 am
Newsgroups: microsoft.public.excel.programming
From: Laurent Longre <lon...@wanadoo.fr>
Date: 1998/12/04
Subject: Re: [a1] syntax
Bonjour,

In VBA, [Anything] is equivalent to Application.Evaluate("Anything")
(Application is optional). The only difference is that the expression
between [] is "hard-coded", whereas the argument of the Evaluate
function can be variable (Evaluate(MyVariable), Evaluate(Var1 & Var2)
etc).

Evaluate("Anything") and [Anything] evaluates and returns the result of
"Anything" as if it were used in a worksheet. The argument can be a
formula involving cell references and built-in or add-in worksheet
functions, for instance:

[SUM(1,2,4,8)] returns 19

[AVERAGE(A1:A100)/Sheet2!A1] returns the same result as the
corresponding worksheet formula (in this case, A1:A100 refers to the
active sheet).

Since worksheet formulae can return not only values but also range
references, so the Evaluate/[] function. For instance, [A1] returns a
range reference, because the equivalent formula =A1 entered in a cell
returns also internally a range reference (which is automatically
coerced into its value). Of course, you can use this reference exactly
like any "true" VBA Range object:

[Sheet3!A1] = 5 puts '5' in the range Sheet3!A1 (you could also use
[Sheet3!A1].Value = 5, but .Value is the default property for a Range
object).

Set MyRange = [OFFSET(A1:C30,3,3)] stores a reference to the range
D4:F33 of the active sheet in the "MyRange" variable.

... and so on.

Evaluate/[] can also return the result of an array formula (exactly like
the EVALUATE() XLM function, on which it is probably based), for
instance:

[SUM((A1:A100>10)*(B1:B100<50)] returns the same result as the array
formula {=SUM((A1:A100>10)*(B1:B100<50))} (ie the number of rows
matching the criterias Ax > 10 and Bx < 50)

MyArray = [ROW(1:10000)] stores the 2 dimensional array (1 column)
{1;2;3;4;...;9999;10000} in MyArray (variant). No need to say that it is
much more efficient than filling MyArray with a 'For I=1 To 10000 ...
Next' loop.

[A1:A5000] = [ROW(1:5000)] puts the values 1, 2, ..., 5000 in the range
A1:A5000.

This feature allows VBA user-defined functions to use (to a certain
extent) array formulae, for instance the following function, which
returns the cumulated number of characters contained in a range:

Function NBCHAR(MyRange As Range) As Long
  NBCHAR = Evaluate("SUM(LEN(" & _
    MyRange.Address(external:=True) & "))")
End Function

This function is equivalent to the array formula {=SUM(LEN(Range))}.
It's of couse much more efficient than the "pure" VBA function:

Function NBCHAR2(MyRange As Range) As Long
  Dim MyCell As Range
  For Each MyCell In MyRange
    NBCHAR2 = NBCHAR2 + Len(MyCell)
  Next MyCell
End Function

If you test both functions and pass to them references to very large
ranges, you will see that the evaluated array formula in NBCHAR runs
much faster than the For... Next loop in NBCHAR2.

Unfortunately you can't use VBA arrays in this way, but otherwise it's a
powerful method which can be applied of course also in VBA Subs.

Hope this helps,

Laurent

Bill Koran wrote:

> Please defer to Mr. Beban's coming (I hope) reply, because I'm a newbie to
> VBA (how long can I claim that?), but here is my understanding.
> Set rng = showcell.[a1]

> is exactly equivalent to

> Set rng = showcell.("a1")

> In other words, anyplace you would refer to a range using the parentheses
> and quotes you can, instead, use the square brackets.  (I think I found
> this info on Chip Pearson's web page.)

> Bill
> --
> Bill Koran, P.E.
> Energy Conservation Engineer

> Brian Wilson <b...@ccms.net> wrote in article <3666BFD8.B4FAE...@ccms.net>...
> > Alan,

> > I've read you're postings this week.  I'm curious, would
> > you explain the [a1] syntax in

> >     Set rng = showcell.[a1]

> > Is Range(rng(2, 1), rng(101, 35)) the best "shorthand"
> > alternative for the cells method of referring to what I would
> > call a subset of a range (which is more than one item)?

> > Thanks,

> > Brian

> > Alan Beban wrote:

> > > John Green wrote:

> > > > This will work when the referenced worksheet is active, not
> otherwise.
> > > > The problem is that the Cells method refers by default to the active
> > > > sheet. You should use something like:

> > > > With showcell
> > > >     .Range(.Cells(2, 1), .Cells(101, 35)).ClearContents
> > > > End With

> > > The above involves "qualifying" the Cells Method/Property. One can also
> > > use the following, because the object variable (rng) carries its own
> > > "built-in" qualification; i.e., its own "built-in" implicit reference
> to
> > > the relevant sheet:

> > > Set rng = showcell.[a1]
> > > Range(rng(2, 1), rng(101, 35)).ClearContents

> > > Sorry, John; after our recent exchange of comments, I couldn't resist
> > > jumping in :-)

> > > Alan Beban be...@pacbell.net


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google