Page 1 of 10 123 ... LastLast
Results 1 to 10 of 98

Thread: VBA Evaluate Range and VLOOKUP

  1. #1
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA Evaluate Range and VLOOKUP

    Hi,
    As a beginner I may be going a bit above my head!!

    After studying and participating at some considerable length in Threads and links to do with using the Evaluate function to speed things up, I thought I understood it. Here are some of those links and Threads. (www.excelfox.com/forum/f22/concatenating-balls-1891/ VBA Trick of the Week :: Avoid Loop for Range Calculations – Evaluate | Useful Gyaan )

    . So … the following simplified example File (XL2007 .xlsm)
    https://app.box.com/s/pr78mhna00advvhsrmvi
    has a Spreadsheet LEFT Function and a Spreadsheet VLOOKUP Function

    The results look good! (That is to say wot I expect!) :-


    ABCD
    1Produnt
    2Name
    3Chocolate-europe aroma4Choc
    4Chocolate-Cookies0Choc
    5Banana-Chocolate-Split10Bana
    6Limette-Käsekuchen16Lime
    7Erdbeere-Quark8Erdb
    8Erdbeere-Mix0Erdb
    9Jamaica Sun6Jama
    10Waldbeeren0Wald
    11
    12
    13
    14LOOKUP Table
    15Product Name
    16Haselnuß-Walnuß-aromatisiert
    17Tiramisu2
    18Chocolate-colonial blend
    19Chocolate-europe aroma4
    20Chocolate-Cookies
    21Jamaica Sun6
    22Himbeere-Joghurt
    23Erdbeere-Quark8
    24Erdbeere-Mix
    25Banana-Chocolate-Split10
    26Waldbeeren
    27Kirsche12
    28Kirsche-grüner Apfel
    29Kirsche-Ananas14
    30Stracciatella
    31Limette-Käsekuchen16
    32grüner Apfel-Quark
    33Blutorange-Quark

    Sheet1



    Worksheet Formulas
    CellFormula
    B3=VLOOKUP(A3,$A$16:$C$33,3,FALSE)
    B4=VLOOKUP(A4,$A$16:$C$33,3,FALSE)
    B5=VLOOKUP(A5,$A$16:$C$33,3,FALSE)
    B6=VLOOKUP(A6,$A$16:$C$33,3,FALSE)
    B7=VLOOKUP(A7,$A$16:$C$33,3,FALSE)
    B8=VLOOKUP(A8,$A$16:$C$33,3,FALSE)
    B9=VLOOKUP(A9,$A$16:$C$33,3,FALSE)
    B10=VLOOKUP(A10,$A$16:$C$33,3,FALSE)
    D3=LEFT(A3,4)
    D4=LEFT(A4,4)
    D5=LEFT(A5,4)
    D6=LEFT(A6,4)
    D7=LEFT(A7,4)
    D8=LEFT(A8,4)
    D9=LEFT(A9,4)
    D10=LEFT(A10,4)





    I apply this code

    Code:
    Sub Evaluate_Left()Dim rngName As Range
    Set rngName = ThisWorkbook.Worksheets("Sheet1").Range("A3:A10")
    Dim rngEE As Range
    Set rngEE = ThisWorkbook.Worksheets("sheet1").Range("E3:E10")
    Let rngEE = Evaluate("if(row(3:10),LEFT(" & rngName.Address & ",4))")
    End Sub 'Evaluate_Left()

    And get the following:-

    ABCDE
    1Produnt
    2Name
    3Chocolate-europe aroma4ChocChoc
    4Chocolate-Cookies0ChocChoc
    5Banana-Chocolate-Split10BanaBana
    6Limette-Käsekuchen16LimeLime
    7Erdbeere-Quark8ErdbErdb
    8Erdbeere-Mix0ErdbErdb
    9Jamaica Sun6JamaJama
    10Waldbeeren0WaldWald
    11
    12

    Sheet1





    Which again is wot I expect.


    Now I apply this code

    Code:
    Sub Evaluate_VLOOKUP()Dim rngName As Range
    Set rngName = ThisWorkbook.Worksheets("Sheet1").Range("A3:A10")
    Dim rngCC As Range
    Set rngCC = ThisWorkbook.Worksheets("sheet1").Range("C3:C10")
    Let rngCC = Evaluate("if(row(3:10),VLOOKUP(" & rngName.Address & ",$A$16:$C$33,3,FALSE))")
    End Sub 'Evaluate_VLOOKUP()

    …but get the following:-


    ABCDE
    1Produnt
    2Name
    3Chocolate-europe aroma44ChocChoc
    4Chocolate-Cookies04ChocChoc
    5Banana-Chocolate-Split104BanaBana
    6Limette-Käsekuchen164LimeLime
    7Erdbeere-Quark84ErdbErdb
    8Erdbeere-Mix04ErdbErdb
    9Jamaica Sun64JamaJama
    10Waldbeeren04WaldWald
    11
    12

    Sheet1





    . I would have expected that result without The extra If Row() stuff, which I thought overcame this problem.
    . can anyone suggest wot is going wrong, or how I obtain the correct results (Using the Evaluate Function for a range)
    Thanks
    Alan.

    P.s. I will also post this Thread Here: Multiple Columns Into Single Column Using Data Text To Column - Page 2

  2. #2
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    19,706
    Post Thanks / Like
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Hi Alan

    Interesting question.

    First, the way I'd do it would be, for ex., using your formula

    Code:
    With rngCC
             .Formula = "=VLOOKUP(" & rngName(1, 1).Address(0, 0) & ",$A$16:$C$33,3,FALSE)"
             .Value = .Value
    End With
    or calling VlookUp() in vba:

    Code:
    rngCC = Application.WorksheetFunction.VLookup(rngName, Range("A16:C33"), 3, False)
    Quote Originally Posted by DocAElstein View Post
    . I would have expected that result without The extra If Row() stuff, which I thought overcame this problem.
    . can anyone suggest wot is going wrong, or how I obtain the correct results (Using the Evaluate Function for a range)
    I can give you my take on this.


    The way I see it, you are not getting the results you want simply because they are not there.

    The "if(row()" or "if(column()" workarounds in the Evaluate do not create answers, they just remind the vba Evaluate() to get all the results that the formula in the worksheet returns and not just one.

    If the results are not there you cannot get them.

    I'll try to explain what I mean with 2 examples.

    Example 1 - the evaluation of the formula returns an array

    Some formulas may return an array when you evaluate them in the worksheet but when you use Evaluate() in vba you just get 1.

    An example. Let's say B1 holds a Text value and B2 a number value. Write in A1:

    =ISTEXT(B1:B2)

    Although you only get in the cell 1 result (True/False), you are examining 2 cells, and if in the formula bar you select the formula and press F9 you see that the result is, in fact, the array {TRUE;FALSE}. You are seeing 1 result in the worksheet cell but the formula generated an array with 2 results.

    Now if in vba you use:

    Code:
    Dim v As Variant
    
    v = Evaluate("ISTEXT(B1:B2)")
    If you execute this statement and inspect v you'll see that it is Variant/Boolean with the value True.

    In this case you already saw in the worksheet that the formula returns an array. To tell Evaluate() to loop through the range you use the "if(row()". This way, since row() always returns an array, Evaluate() will go through all the values.

    Code:
    Dim v As Variant
    
    v = Evaluate("IF(ROW(B1:B2),ISTEXT(B1:B2))")
    Now if you inspect v you see it's a Variant/Variant(1 to 2, 1 to 1) and you get the array with the values True and False

    The "if(row()" did not create the other result, it just helped bringing it back.


    Example 2 - the evaluation of the formula does not return an array, it returns a simple value

    This is the case for ex. of

    =VLOOKUP(A1:A2,B1:C3,2,FALSE)

    Now if you add this formula to a cell with some values in A1:A2 and B1:C3 you'll see 1 value in the cell.

    If you now do as in the previous example and select the formula in the formula bar and press F9 you'll see that the formula only returns that 1 value. It does not return an array although you might think it would since the first parameter is an array.


    Now this is my point. When, like in this case, the formula does not return an array there's no use in Evaluating in vba using the "If(Row()".

    There is no other value to get. You'll just get the same value twice

    If you try:

    Code:
    Dim v As Variant
    
    v = Evaluate("=VLOOKUP(A1:A2,B1:C3,2,FALSE)")
    v = Evaluate("=IF(ROW(A1:A2),VLOOKUP(A1:A2,B1:C3,2,FALSE))")
    and inspect v after each Evaluate() statement you'll see that exactly.

    Notice that this would also be valid for others formula, like:
    =INDEX(A1:A3,{1;3;2},1)

    Also in this case the same as with the VLookUp(). If you evaluate the formula in the worksheet in the formula bar with F9 you see that there is only 1 value returned from the formula, so there's no point in using the If(row() in vba. You'll just get the same value thrice.


    Conclusion:

    If the formula evaluated in the worksheet returns an array of values but in vba is only returning 1 value, you can use "if(row()" or "if(column()" bits to tell vba to get all the results.

    If the formula evaluated in the worksheet returns only 1 value that's all you can get


    Remark: I don't mean by this that the formulas could not return more than one value, they could if used as array formulas that return an array, or using some workaround, but not as simple native formulas inserted directly in a cell like we did in these examples.

    Please comment.
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

  3. #3
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Hi pgc,

    Re:- VBA Evaluate Range VLOOKUP

    Quote Originally Posted by pgc01 View Post
    Hi Alan

    Interesting question.

    First, the way I'd do it ………
    …..
    . …………………….

    I can give you my take on this…….
    ……
    . …………….
    Please comment.

    . Thanks very much for taking the trouble to reply in such detail. (I thought this one had been lost and buried under the amazing number of threads that go through this forum!).
    . I have to be away from my (Excel) computers just now. As soon as I can I will go carefully through everything you have kindly written and get back to you. (By replying (only) to this Thread/Post)

    . Thanks Again
    . Alan Elston
    Bavaria

  4. #4
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Hi pgc,

    Re:- VBA Evaluate Range VLOOKUP

    Quote Originally Posted by pgc01 View Post
    ….
    First, the way I'd do it would be, for ex., using your formula


    Code:
    Sub Test3b_pgc()
     
    Dim rngName As Range
    Set rngName = ThisWorkbook.Worksheets("Sheet1").Range("A3:A10")
    Dim rngCC As Range
    Set rngCC = ThisWorkbook.Worksheets("sheet1").Range("C3:C10")
    With rngCC
             .Formula = "=VLOOKUP(" & rngName(1, 1).Address(0, 0) & ",$A$16:$C$33,3,FALSE)"
    '         .Value = .Value
    End With
     
    End Sub
    ….
    …..
    ...
    . This has been a learning exercise for me (Which maybe I tried to take a (5th) bridge too far for my beginner’s competence by looking at a further 5th Evaluate method!!). This thread followed on from thread http://www.mrexcel.com/forum/excel-q...ml#post3937559 where the third method kindly suggested by Jerry Sullivan follows very closely your method suggested in the above code.

    Code:
    Sub Test3UsingR1C1JerrySullivan() 'enters R1C1 formula into results range then converts formulas to values Dim rngDD As Range
     Set rngDD = ThisWorkbook.Worksheets("sheet1").Range("D3:D10")
     Dim rngLOOKUP As Range
     Set rngLOOKUP = ThisWorkbook.Worksheets("sheet1").Range("$A$16:$C$33")
    
    
     With rngDD
       .FormulaR1C1 = "=VLOOKUP(R[0]C[-3]," & rngLOOKUP.Address(ReferenceStyle:=xlR1C1, External:=True) & ",3,0)"
       'Jerry put my exact SVERWEISS formula in!
       '. Syntax: FormulaR1C1=" here the formula ". The [] makes it relative referrencing.
     '  .Value = .Value 'Removes Formula(Puts value in)
     End With
    
    
    End Sub

    As that was my very first experience with the .Formula RC stuff, it is very helpful again to see a slightly different version of this method. A great helper again in my learning. Thanks!


    Quote Originally Posted by pgc01 View Post
    ….
    …or calling VlookUp() in vba:

    Code:
    Sub Test3c_pgc()
     
    Dim rngName As Range
    Set rngName = ThisWorkbook.Worksheets("Sheet1").Range("A3:A10")
    Dim rngEE As Range
    Set rngEE = ThisWorkbook.Worksheets("sheet1").Range("E3:E10")
     
    Let rngEE = Application.WorksheetFunction.VLookup(rngName, Range("A16:C33"), 3, False)
     
    End Sub
    ….

    ...
    … and once again a very helpful comparison. Many Thanks.

    ABCDE
    1Produnt
    2NameAlanpgcJerry SullivanpgcVBA
    3Chocolate-europe aroma4444
    4Chocolate-Cookies000
    5Banana-Chocolate-Split10101010
    6Limette-Käsekuchen16161616
    7Erdbeere-Quark8888
    8Erdbeere-Mix000
    9Jamaica Sun6666
    10Waldbeeren000

    Sheet1



    Worksheet Formulas
    CellFormula
    B3=VLOOKUP(A3,$A$16:$C$33,3,FALSE)
    C3=VLOOKUP(A3,$A$16:$C$33,3,FALSE)
    D3=VLOOKUP(A3,Sheet1!$A$16:$C$33,3,0)
    B4=VLOOKUP(A4,$A$16:$C$33,3,FALSE)
    C4=VLOOKUP(A4,$A$16:$C$33,3,FALSE)
    D4=VLOOKUP(A4,Sheet1!$A$16:$C$33,3,0)
    B5=VLOOKUP(A5,$A$16:$C$33,3,FALSE)
    C5=VLOOKUP(A5,$A$16:$C$33,3,FALSE)
    D5=VLOOKUP(A5,Sheet1!$A$16:$C$33,3,0)
    B6=VLOOKUP(A6,$A$16:$C$33,3,FALSE)
    C6=VLOOKUP(A6,$A$16:$C$33,3,FALSE)
    D6=VLOOKUP(A6,Sheet1!$A$16:$C$33,3,0)
    B7=VLOOKUP(A7,$A$16:$C$33,3,FALSE)
    C7=VLOOKUP(A7,$A$16:$C$33,3,FALSE)
    D7=VLOOKUP(A7,Sheet1!$A$16:$C$33,3,0)
    B8=VLOOKUP(A8,$A$16:$C$33,3,FALSE)
    C8=VLOOKUP(A8,$A$16:$C$33,3,FALSE)
    D8=VLOOKUP(A8,Sheet1!$A$16:$C$33,3,0)
    B9=VLOOKUP(A9,$A$16:$C$33,3,FALSE)
    C9=VLOOKUP(A9,$A$16:$C$33,3,FALSE)
    D9=VLOOKUP(A9,Sheet1!$A$16:$C$33,3,0)
    B10=VLOOKUP(A10,$A$16:$C$33,3,FALSE)
    C10=VLOOKUP(A10,$A$16:$C$33,3,FALSE)
    D10=VLOOKUP(A10,Sheet1!$A$16:$C$33,3,0)




    All codes up to now in Sheet1 Module of Example File



    …………………………………………..

    Quote Originally Posted by pgc01 View Post
    ….
    … Example 1…..
    Let's say B1 holds a Text value and B2 a number value. Write in A1:

    =ISTEXT(B1:B2)

    Although you only get in the cell 1 result (True/False), you are examining 2 cells, and if in the formula bar you select the formula and press F9 you see that the result is, in fact, the array {TRUE;FALSE}. You are seeing 1 result in the worksheet cell but the formula generated an array with 2 results……..
    ………etc……

    Code:
    
    Sub RowColumnTrick1()
     
    Dim v As Variant
    Let v = Evaluate("ISTEXT(B1:B2)")
    Dim vIfRow() As Variant
    vIfRow() = Evaluate("IF(Row(B1:B2),ISTEXT(B1:B2))")
    Dim vIfColumn() As Variant
    vIfColumn() = Evaluate("IF(Column(B1:B2),ISTEXT(B1:B2))")
    End Sub
    ….
    …etc…..
    …. Interesting example to watch in watch window! Sort of shows about as far as I had got originally understanding the Row / Column trick Stuff! (The following is in sheet 2 (and Macro in Sheet2 Module)of my example File?

    AB
    1WAHRText
    21

    Sheet2



    Worksheet Formulas
    CellFormula
    A1=ISTEXT(B1:B3)




    ………………………….

    ...
    Example 2…………….!

    [QUOTE=pgc01;3944863]….

    . It is going to take my Beginner’s brain some time to do justice to your efforts and understand and comment on this!

    . I shall begin to do that (Possibly try to adapt it to my example) and hopefully be able to comment further sometime later!


    . Thanks Again
    . Alan Elston

    P.s. My learning File again modified (to my current stage so far!!) : (XL2007 .xlsm)
    https://app.box.com/s/biav19uhby7g4ji4t3gz

  5. #5
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    …………………………………………..



    Last Bit again hopefully in correct Form!!

    Quote Originally Posted by pgc01 View Post
    ….
    … Example 1…..
    Let's say B1 holds a Text value and B2 a number value. Write in A1:

    =ISTEXT(B1:B2)

    Although you only get in the cell 1 result (True/False), you are examining 2 cells, and if in the formula bar you select the formula and press F9 you see that the result is, in fact, the array {TRUE;FALSE}. You are seeing 1 result in the worksheet cell but the formula generated an array with 2 results……..
    ………etc……

    Code:
    
    Sub RowColumnTrick1()
     
    Dim v As Variant
    Let v = Evaluate("ISTEXT(B1:B2)")
    Dim vIfRow() As Variant
    vIfRow() = Evaluate("IF(Row(B1:B2),ISTEXT(B1:B2))")
    Dim vIfColumn() As Variant
    vIfColumn() = Evaluate("IF(Column(B1:B2),ISTEXT(B1:B2))")
    End Sub
    ….
    …etc…..

    ...
    …. Interesting example to watch in watch window! Sort of shows about as far as I had got originally understanding the Row / Column trick Stuff! (The following is in sheet 2 (and Macro in Sheet2 Module)of my example File?

    AB
    1WAHRText
    21

    Sheet2



    Worksheet Formulas
    CellFormula
    A1=ISTEXT(B1:B3)




    ………………………….

    Quote Originally Posted by pgc01 View Post
    ….

    Example 2…………….!

    ...
    . It is going to take my Beginner’s brain some time to do justice to your efforts and understand and comment on this!

    . I shall begin to do that (Possibly try to adapt it to my example) and hopefully be able to comment further sometime later!


    . Thanks Again
    . Alan Elston

    P.s. My learning File again modified (to my current stage so far!!) : (XL2007 .xlsm)
    https://app.box.com/s/biav19uhby7g4ji4t3gz

  6. #6
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Hi again PGC

    Quote Originally Posted by pgc01 View Post
    …...

    ….Example 2 - the evaluation of the formula does not return an array, it returns a simple value..
    . ……..
    …………………..
    . ……….

    Also in this case the same as with the VLookUp(). If you evaluate the formula in the worksheet in the formula bar with F9 you see that there is only 1 value returned from the formula, so there's no point in using the If(row() in vba. You'll just get the same value thrice.


    Conclusion:

    If the formula evaluated in the worksheet returns an array of values but in vba is only returning 1 value, you can use "if(row()" or "if(column()" bits to tell vba to get all the results.

    If the formula evaluated in the worksheet returns only 1 value that's all you can get


    Remark: I don't mean by this that the formulas could not return more than one value, they could if used as array formulas that return an array, or using some workaround, but not as simple native formulas inserted directly in a cell like we did in these examples.

    Please comment.
    .

    …………….OK. I am following you. If I modify a version of my very original Table, that is to say change the formulas in the third row as follows:

    ABCDEF
    1Produnt
    2Name
    3Chocolate-europe aroma4Choc
    4Chocolate-Cookies0Choc
    5Banana-Chocolate-Split10Bana

    Sheet1



    Worksheet Formulas
    CellFormula
    B3=VLOOKUP(A3:A10,$A$16:$C$33,3,FALSE)
    B4=VLOOKUP(A4,$A$16:$C$33,3,FALSE)
    B5=VLOOKUP(A5,$A$16:$C$33,3,FALSE)
    F3=LEFT(A3:A10,4)
    F4=LEFT(A4,4)
    F5=LEFT(A5,4)



    And then follow your idea applied to my modified Formulas:

    Quote Originally Posted by pgc01 View Post
    ….

    and if in the formula bar you select the formula and press F9 you see that the result is,…


    ...
    Then I see the following

    For VLOOKUP: 4

    For LEFT: ={"Choc";"Choc";"Bana";"Lime";"Erdb";"Erdb";"Jama";"Wald"} (; instead of , as I am in German Excel)

    . So it all ties up.
    . I still am wondering why some formulas do not produce an array. Is it just “pot luck”?
    . I would still be grateful if anyone out there can come up with a way to…..

    Quote Originally Posted by pgc01 View Post
    ….
    Remark: I don't mean by this that the formulas could not return more than one value, they could if used as array formulas that return an array, or using some workaround, but not as simple native formulas
    ...
    …. Workaround to get my VLOOKUP to return an array!


    . If I come up with any ideas, or have any further contributions to this thread as I attempt to find a solution then I will report back!

    Many Thanks again
    Alan Elston
    Bavaria
    Germany

  7. #7
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Quote Originally Posted by DocAElstein View Post
    .........a";"Lime";"Erdb";"Erdb";"Jama";"Wald"} (; instead of , as I am in German Excel)
    ......
    Oops... ignor that little bit . Unusually I think this time it may be the same in English and German Excel

    Alan

  8. #8
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    19,706
    Post Thanks / Like
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Quote Originally Posted by DocAElstein View Post
    . I would still be grateful if anyone out there can come up with a way to…..

    …. Workaround to get my VLOOKUP to return an array!
    Hi Alan

    As you may know, functions like Index() and Vlookup() that seem to return only 1 value (when evaluated in the formula bar), can, in fact, return an array of values when used as an array formula that returns an array applied to a range of cells.

    For ex, in post #6, if you use the VLookUp() as an array function that returns an array, like

    - select B3:B5
    - in the formula bar paste: =VLOOKUP(A3:A5,$A$16:$C$33,3,FALSE)
    - confirm with Control-Shift-Enter

    You'll see that the 3 correct results are returned. That's how VLookUp() behaves when applied as an array formula that spans more than 1 cell.

    This means that although when you evaluate the VLookUp() you only see 1 result, the other results are somehow possible to be generated.


    The workaround I was thinking was one published by XOR LX article here:

    INDEX: Returning an array of values | EXCELXOR

    where he presents a workaround for the case of these functions like Index() and Vlookup() that seem to return only 1 value (when evaluated in the formula bar), but that in fact can return an array of values when used as an array formula that returns an array applied to a range of cells.

    This is, of course, very interesting as a learning experience, but I would not forget the simplest solution. Writing the formula in the cells and let excel evaluated it, like

    Code:
    With range
             .Formula = someformula
             .Value = .Value
    End With
    ... simple and easy to read and understand.


     ABCDEFG
    1Produnt      
    2Name      
    3Chocolate-europe aroma4   Choc 
    4Chocolate-Cookies0   Choc 
    5Banana-Chocolate-Split10   Bana 
    6       
    [Book1]Sheet1




    AddrFormula
    [Book1]Sheet1
    B3:B5 =VLOOKUP(A3:A5,$A$16:$C$33,3,FALSE)
    This in an array formula that is returning an array. Select the WHOLE range, paste the formula into the formula bar
    and confirm with CTRL+SHIFT+ENTER and not just ENTER
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

  9. #9
    Board Regular XOR LX's Avatar
    Join Date
    Jul 2012
    Location
    Turin, Italy
    Posts
    4,517
    Post Thanks / Like
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Quote Originally Posted by pgc01 View Post
    The workaround I was thinking was one published by XOR LX article here:

    INDEX: Returning an array of values | EXCELXOR

    where he presents a workaround for the case of these functions like Index() and Vlookup() that seem to return only 1 value (when evaluated in the formula bar), but that in fact can return an array of values when used as an array formula that returns an array applied to a range of cells.
    Actually PGC the technique I outline there is neither for applying over a range of cells, nor is it even an array formula.

    It is rather a method by which we can use a single-cell, non-array formula to generate an array of returns to be passed to some function, e.g. INDEX, VLOOKUP, which "normally" do not behave as such.

    So as in one of my examples there, something like:

    =MAX(VLOOKUP(T(IF(1,{"A","B","C"})),J1:K10,2,0))

    which gives the maximum value in K1:K10 where the corresponding row entry in J1:J10 is the first occurrence in that range of either "A", "B" or "C", is a single-cell formula which resolves to:

    =MAX(VLOOKUP({"A","B","C"},J1:K10,2,0))

    which is e.g.:

    =MAX({1,2,4})

    where the VLOOKUP has been coerced into operating over an array of values.

    Hope that clarifies things a bit!

    Regards
    Advanced Excel Techniques: http://excelxor.com/

  10. #10
    MrExcel MVP
    Join Date
    Apr 2006
    Posts
    19,706
    Post Thanks / Like
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA Evaluate Range and VLOOKUP

    Sorry I think (or, in this case, I know )I did not make myself clear.

    Your workaround is not needed if you apply the formula over a range of cells.
    Like in the example I posted in post #8, if we apply the formula over a range of cells there's no need to use any workaround, the formula works fine directly.

    Alan's problem is that he wants to use vba Evaluate() to get those "other" values that you'd get directly if the formula was used on a range of cells.
    There's where your workaround comes handy.
    Using your workaround you can get from a formula in a single cell all the results that you'd get using the formula directly over a range of cells.

    This means that this way we can use it in Evaluate() to get all the results and that solves Alan's problem.

    For my example in post #8, using your workaround in the VLookUp() formula that I used in cells B3:B5, we can get all the results in the Evaluate().

    Hope it's clearer now.
    Kind regards
    PGC

    To understand recursion, you must understand recursion.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •