PDA

View Full Version : Solved: The use of [] brackets for assigning values or arrays



DoLoop
05-22-2008, 01:30 AM
Hi,

I've just discovered that assigning values from cells or a range of cells to single variables or arrays can be simply done by following code:


Sub AssigningValues ()

Dim a, b, c, d

a = [A1] 'single value referring to cell A1
b = [A1:A10] '2D array
c = [transpose(A1:A10)] '1D array
d = [test] 'in case cell A1 has name "test"

End sub


Now, this looks a better method to me, than using

.Cells(1,1)
or
.Range("A1").Value

Can anyone tell me if this method is faultless? Or do I have to take into account some surprises?

Thx.

Bob Phillips
05-22-2008, 01:43 AM
It is well documented here in VBAX that I think it is sloppy, poor programming, and I think it shuld not be used.

I would argue that it is slower than the the correct form, as it has to be evaulated. Others argue the reverse, but my tests show otherwise.

Bob Phillips
05-22-2008, 01:44 AM
Similalrly, I think this is sloppy, poor programming



Dim a, b, c, d

MattKlein
05-22-2008, 11:59 AM
I agree that not declaring your variable types up front is bad programming form. For me, once a program gets past a few pages of code I spend just as much time, if not more, debuging it than it took to actually type the initial code. By declaring variable types up front, you are starting off on the right foot for quick debuging and it only costs you an extra few seconds worth of key strokes.

david000
05-22-2008, 10:07 PM
Johnske: Evaluate artical - really good!
http://xlvba.3.forumer.com/index.php?showtopic=278

DoLoop
05-22-2008, 11:27 PM
I must admit that the declaring part is not the way to do. But this wasn't the issue for me. I only wanted to show the brackets.
David,
Thanks for the article. I've learnt a lot again!