 # Square Brackets - Vba Notation

• Hi All,

I often see square brackets around ranges in code. eg [A1]

Can anyone tell me if there are any advantages/disadvantages with using this notation, apart from the obvious its shorter?

Cheers.

• Re: Square Brackets - Vba Notation

I can tell you at least one disadvantage - A1 will be A1 on the active sheet.

Now that may or may not be what you want but generally I would say it's better to reference worksheets explicitly in code.

Boo!:yikes:

• Re: Square Brackets - Vba Notation

Hi Norie,

So you are saying if you dont use worksheet("ShtNm") in front of it then it will select the activesheet. Wont doing that with range do the same thing?

• Re: Square Brackets - Vba Notation

Anytime you don't qualify a range reference using whatever notation then VBA will use the active sheet.

I've lost count of the number of problems that have been solved by just correctly referencing things.

And doing that isn't hard, it could be as simple as adding the . dot qualifier.

And using With...End With can be handy too.

Boo!:yikes:

• Re: Square Brackets - Vba Notation

You may want to read through this post for some additional insight.

Regards,

• Re: Square Brackets - Vba Notation

You're welcome.

• Re: Square Brackets - Vba Notation

Yes, activesheet is assumed for both bracket and Range notation. Maybe these examples will help. Press Ctrl+G to view the results in the Immediate window.

• Re: Square Brackets - Vba Notation

[ ] represents Evaluate method

[A1] = Evaluate("A1")

so, you can write like

[Sheet1!A1]

and you could do something like this with evaluate method

Code
``````Sub test()
With Range("a1:A10")
.Value = [{1;2;3;4;5;6;7;8;9;10}]
.Offset(,1).Value = [A1:A10+5]
.Offset(,1).Resize(1).Value = Join([transpose(a1:a10)],",")
End With
End Sub``````
• Re: Square Brackets - Vba Notation

Making more and more sense.

I was confused at the start because I thought the brackets were just a shortcut for writing Range. It actually means evaluate.

Thanks Jindon

• Re: Square Brackets - Vba Notation

I guess I would interpret []'s to mean both Range and Evaluate depending on use.

Using jindon's example, we can use more brackets.

Code
``````Sub test2()
With [a1:A10]
.Value = [{1;2;3;4;5;6;7;8;9;10}]
.Offset(, 1).Value = [A1:A10+5]
.Offset(, 1).Resize(1).Value = Join([transpose(a1:a10)], ",")
End With
End Sub``````

## Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!