Brackets [] possibilities ?

A

alainB

Hi,

If I want to put the number 6 in the cell A1 up to now I was doing i
this way:

Range("A1").Select
ActiveCell.FormulaR1C1 = 6

I learn from one post here that I can do it much more easily this way:

[A1] = 6

As long as I just want to put a specific value in one particular cell
should I do it this way?

Thanks

Alai
 
C

Chip Pearson

Alain,

In some MS documentation (I forget the URL, probably somewhere in
MSDN), it was noted that using the [] syntax is significantly
slower than using the Range() syntax. I suspect, but don't know
for a fact, that everything inside the [] is unknown to the
compiler at compile time, and has to be resolved at run time.
This causes extra code to be generated at compile time and
executed at run time.

You should never use the syntax in your first example:
Range("A1").Select
ActiveCell.FormulaR1C1 = 6

There is no reason to Select the range before operating upon it,
and Select is an expensive operation. Instead, combine the two
lines in to a single line of code like

Range("A1").Value = 6

Performance issues aside, whether to use the [] syntax is a
matter of personal taste. I don't like it (I'm not sure why I
don't like it, but I don't), so I always use the Range() syntax.
If you read the posts of the professional developers in this
newsgroup, you'll find that nearly everyone uses the Range()
syntax over the [] syntax. However, you are free to develop a
coding style that suits you. Whichever you choose, be consistent;
don't mix and match reference styles.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





alainB > said:
Hi,

If I want to put the number 6 in the cell A1 up to now I was doing it
this way:

Range("A1").Select
ActiveCell.FormulaR1C1 = 6

I learn from one post here that I can do it much more easily this way:

[A1] = 6

As long as I just want to put a specific value in one particular cell,
should I do it this way?

Thanks

Alain
 
R

Rob van Gelder

You could do it this way and it would be faster than doing .Select

Range("A1").Value = 6 is faster than [A1] = 6 though.
 
Top