• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Question - references named ranges

tobediff

New Member
A question - I have traditionally used the following method for referencing named ranges in VBA.
Code:
Sub HeyListen()
   Dim navi as String
   navi = Range("message").Value
   MsgBox navi
End Sub

I saw this method employed recently.
Code:
Sub HeyListen()
   Dim navi as String
   navi = [message]
   MsgBox navi
End Sub

Is there any substantial difference between them? The only thing I can come up with is the ability to insert variables into my choice of named range is only possible with the first method.
Example:
Code:
Sub HeyListen()
   Dim navi as String
   navi = ""
   For i = 1 to 3
      navi = navi & Range("message" & i).Value
   Next
   MsgBox navi
End Sub
This works, but I don't see how you could do it using the square bracket method.
 
You are correct. Using the brackets is shorthand approach you can use to write ranges. Examples:
[A1]
[NamedRange]

A similar thing is making comments. You can do:
'This is a comment
or
Rem This is a comment

It has to do with all the VB languange has grown from different languages over the years. Some would say use whatever works best for you. My personal nature is to write out Range, because it helps me keep track of what it is, and people don't always know what the brackets are. On the other hand, I can see how using teh brackets is certainly faster to write. :)
 
@tobediff
Hi!
You should consider that Luke M was raised in the school of the:
Let A = B * 2
and only after making big efforts to abandon the line numbering practice, he still misses the Renum instruction.
Regards!
 

Hi, brackets are themselves a short link of the Evaluate method (just see its help) …

Regards !​
 
Back
Top