......
What are the advantages/disadvantages to using this method ?
Dangers could be that there is a lot more to it than meets the eye at first glance. I try to elaborate on Rory’s point....( along the way..).
The square bracket pair
[]
is often said to be the shorthand of the Evaluate Method
Evaluate(“ “)
Consider the following codes. Initially Taking it back a stage, rather than selecting a Range, use
VBA Range(" "),
Evaluate(" "), and
[] to try getting the Cell contents from Cell A1
Using Excel 2007
[Table="width:, class:grid"][tr][td]-[/td][td]A
[/td][/tr]
[tr][td]1
[/td][td]Somefink[/td][/tr]
[/table][Table="width:, class:grid"][tr][td]
Tabelle1[/td][/tr][/table]
You can do that with
Range(" ") and
Evaluate(“ “) , and build up a string as shown in the following codes. The codes also shows however that this cannot be done with the
[] brackets “shorthand”.
With
Evaluate(“ “) one can , build up very complicated strings, including those to make up a Range as the code shows. The point is that you have the ability to combine, as it were,
VBA Code and
Spreadsheet things with
Evaluate(" "). As Rory pointed out the
[] sort of restricts you to code things. Because of the absence of an opening and closing quote in the “definition” as it were of
[] we kind of have in this case
[ object to be evaluated / code world ]
Compared with
Evaluate( string
)
Where the string can then be built up using those in / out pair things
“ & and
& “ which takes us form the string into , for example a
code part.
Evaluate( string
“ & Code world & “ string
)
( The
Code world bit here is as
[] evaluates ) Another way of looking at it, approximately correct is that [] has restricted us to the middle bit here:
Evaluate(“” & ______ & “”)
Similarly
Range( ____ ) is expecting a string argument.
[] is expacting a code thing
If you try to use a Quote in
[ ] it will do just as a code does and tell
VBA to expect a string, and as in the codes return a simple string rather than evaluating that string
It may also be peculiar to
[] that it recognises something like A1 as a range, maybe as it is expecting a code thing.
Codes:
Code:
[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=blue]Sub[/color] strEvalCellContent()
[color=blue]Dim[/color] CellContent [color=blue]As[/color] [color=blue]String[/color]
[color=blue]Dim[/color] strEval [color=blue]As[/color] [color=blue]String[/color] [color=darkgreen]'String to be built up for Evaluate[/color]
[color=blue]Let[/color] CellContent = Evaluate("A1") [color=darkgreen]'This has been evaluated to a range object, hence the next line works[/color]
[color=blue]Let[/color] CellContent = Evaluate("A1").Value [color=darkgreen]'(The line above was an allowed Implicit of this line, that of getting the value when assinging variable to a Cell Range)[/color]
[color=darkgreen]'Let CellContent = Evaluate(A1) ' This errors as it is expecting a string but sees an undefined, unfilled variable[/color]
[color=blue]Let[/color] CellContent = ["Al"] [color=darkgreen]'Gives "A1" and not cell contents[/color]
[color=darkgreen]'Let CellContent = [Al]'Gives incompatible types and..[/color]
[color=blue]Let[/color] CellContent = [A1].Value [color=darkgreen]'...this indicates that [] has evaluated to a range object[/color]
[color=blue]Let[/color] strEval = "A1"
[color=blue]Let[/color] CellContent = Evaluate(strEval)
[color=blue]Let[/color] CellContent = [strEval]
[color=blue]Let[/color] strEval = "A" & 1 & ""
[color=blue]Let[/color] CellContent = Evaluate(strEval)
[color=blue]Let[/color] CellContent = [strEval] [color=darkgreen]'Gives "A1" and not cell contents[/color]
[color=blue]Let[/color] CellContent = Evaluate("A" & 1 & "")
[color=blue]Let[/color] CellContent = Evaluate("A" & 1 & "").Value
[color=blue]Let[/color] CellContent = ["A" & 1 & ""] [color=darkgreen]'Gives "A1" and not cell contents[/color]
[color=blue]End[/color] [color=blue]Sub[/color]
[color=darkgreen]'[/color]
[color=blue]Sub[/color] strEvalRangeSelect()
[color=blue]Dim[/color] strEval [color=blue]As[/color] [color=blue]String[/color] [color=darkgreen]'String to be built up for Evaluate[/color]
Range("A1").Select
Evaluate("A2").Select
[A1].Select [color=darkgreen]'Note: This will not work ["A2"].Select. Error says Requires Object[/color]
[color=blue]Let[/color] strEval = "A2"
Range(strEval).Select
[color=blue]Let[/color] strEval = "A" & 1 & ""
Range(strEval).Select
Evaluate(strEval).Select
Evaluate("A" & 2 & "").Select
[color=darkgreen]'["strEval"].Select 'Run time error Object "Needed" - Here it has been given a String, which you cannot Select[/color]
[color=darkgreen]'[strEval].Select 'Complie error "Invalid identifier" It needs a VBA thing...so[/color]
[color=blue]Dim[/color] rng [color=blue]As[/color] Range: [color=blue]Set[/color] rng = Range("A1") [color=darkgreen] '.. here we define one ( identify rng ) as a Range Object ..and...[/color]
[rng].Select [color=darkgreen]' this will work...![/color]
[color=blue]End[/color] [color=blue]Sub[/color]
Not sure if that helps a bit. It is not the full story, and all a bit complicated and tied up with the confusing world of “Quotes in VBA”. Maybe a bit more detailed explanations here:
http://www.mrexcel.com/forum/excel-questions/696820-quotes-visual-basic-applications.html
Special concatenation - Page 3
Alan