Using Evaluate

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Using Evaluate

Post by YasserKhalil »

Hello everyone
I have this line of code

Code: Select all

a = Application.Index(r.Value, Evaluate("ROW(1:" & r.Rows.Count & ")"), [{4,2,3,6,5,1}])
As far as I know that Evaluate part which is that

Code: Select all

Evaluate(" ..... ")
can be replaced with

Code: Select all

[ ...... ]
But when I tried that change I got error

Code: Select all

a = Application.Index(r.Value, [ROW(1:" & r.Rows.Count & ")], [{4,2,3,6,5,1}])

User avatar
HansV
Administrator
Posts: 78868
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Evaluate

Post by HansV »

You can use [...] to evaluate a fixed expression, but ROW(1:" & r.Rows.Count & ") contains a variable part. And the quotes " aren't correct anymore. Use Evaluate here.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using Evaluate

Post by YasserKhalil »

Thanks a lot for reply
So you mean that if I used this part in that form will be correct

Code: Select all

[Row(1:10)]
I tested that and it worked well
Thanks a lot for the information

User avatar
HansV
Administrator
Posts: 78868
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using Evaluate

Post by HansV »

Yes. If you have a constant expression, you can use [...], but if you have to assemble the expression by concatenating parts, you can't - you must use Evaluate, or use standard VBA.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using Evaluate

Post by YasserKhalil »

Thank you very much Mr. Hans
You are always very very helpful

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Using Evaluate

Post by Doc.AElstein »

Hi YasserKhalil,
Happy New Year.

The Evaluate , especially the “full” version Evaluate(“ “) is a thing that often has caught my interest.

Along the lines of what Hans said, .. The major difference in the two forms , ( the full version Evaluate(“ “) and the “shorthand” version, [ ] ) , is that the “full” version Evaluate(“ “) takes a string as argument which allows you to build up that string with a combination of spreadsheet and VBA things, as was the case in your first post.

You can do some nice one liner codes with Evaluate whereby you evaluate a range . Here you can do some nice “tricks” to make the Evaluate work like an ..Excel array formula in disguise.. You can trick the Evaluate into doing Array calculations, returning you an array of values which you would normally only get with a looping process. Hence a single line can replace a looping process. This can have often result in something working much quicker than in normal VBA looping techniques.

Another way to think about the difference in the two versions:

For a [ ] you ( almost ) put in it exactly what you put in a cell. (The only difference is that you can leave off the = . I expect VBA puts them in but just does not show you. You can always add a = and that is from the syntax accepted ). ( Often people use the look of the square brackets as looking like a cell to remind them of this point. )
You cannot put VBA things in a cell, so you cannot put them in a [ ] either.
Note, for some of the things that you can put into [ ] , you would need to do the CSE Array type entry to get them to “work” if you put them in a cell. You do not need to do that CSE entry when using VBA Evaluate. However you may need to do the clever “tricks” to get the evaluate to do Array calculations. In some ways you can consider that as similar to when you sometimes select a number of cells in a spreadsheet before you do the CSE Entry. So you see from that way of thinking you come to the idea of an .. ..Excel array formula in disguise

For a Evaluate(“ “) you have to arrange that the evaluated string ends up looking like what you have in a cell … that can be a nightmare then with double - doubled quotes and the such.. the tricky way of building up that string, especially where the quotes are concerned***, along with the experience needed to master the “tricks” is probably one reason why this technique has not been used to its full potential. Some of the most experienced people in Excel and VBA will tell you ( possibly not publically ) that they do not understand many of these techniques…

I do not believe that there is any concise documentation from Microsoft on many of the advanced Evaluate techniques. I do not believe any Microsoft person understands the subject very well. The people### who understand these things are few and far between. Catch post from Rick Rothstein, a lot at MrExcel, as one of the few people who seem to be able to master this subject effectively
Alan



Ref:
*** http://www.eileenslounge.com/viewtopic. ... 98#p196259" onclick="window.open(this.href);return false;
### .Excel array formula in disguise… Rick Rothstein, Willy Vanhaelen
http://www.excelfox.com/forum/showthrea ... #post10201" onclick="window.open(this.href);return false;
https://excelribbon.tips.net/T012042_Re ... _Name.html" onclick="window.open(this.href);return false;
https://excelribbon.tips.net/T010768_Sh ... Codes.html" onclick="window.open(this.href);return false;
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

YasserKhalil
PlatinumLounger
Posts: 4936
Joined: 31 Aug 2016, 09:02

Re: Using Evaluate

Post by YasserKhalil »

Thank you very much Mr. Alan for this useful information and merry christmas ..
Best and Kind Regards