=如何使用vlookup函数(,-FIND($A$2:$A$3,D3),$B$2:$B$3)

VBA Evaluate Range and VLOOKUP
Jump to page:
Results 1 to 10 of 98
VBA Evaluate Range and VLOOKUPThis is a discussion on VBA Evaluate Range and VLOOKUP within the Excel Questions forums, part of the Question F Hi,
As a beginner I may be going a bit above my head!!
After studying and participating at some considerable
Bookmark & Share
Linear Mode
Board Regular
VBA Evaluate Range and VLOOKUP
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. (
the following simplified example File (XL2007 .xlsm)
has a Spreadsheet LEFT
Function and a Spreadsheet VLOOKUP Function
The results look good!
(That is to say wot I expect!) :-
ABCD1Produnt2Name3Chocolate-europe aroma4Choc4Chocolate-Cookies0Choc5Banana-Chocolate-Split10Bana6Limette-K?sekuchen16Lime7Erdbeere-Quark8Erdb8Erdbeere-Mix0Erdb9Jamaica Sun6Jama10Waldbeeren0Wald11121314LOOKUP Table15Product Name16Haselnuss-Walnuss-aromatisiert17Tiramisu218Chocolate-colonial blend19Chocolate-europe aroma420Chocolate-Cookies21Jamaica Sun622Himbeere-Joghurt23Erdbeere-Quark824Erdbeere-Mix25Banana-Chocolate-Split1026Waldbeeren27Kirsche1228Kirsche-grüner Apfel29Kirsche-Ananas1430Stracciatella31Limette-K?sekuchen1632grüner Apfel-Quark33Blutorange-QuarkSheet1Worksheet FormulasCellFormulaB3=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
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:-
ABCDE1Produnt2Name3Chocolate-europe aroma4ChocChoc4Chocolate-Cookies0ChocChoc5Banana-Chocolate-Split10BanaBana6Limette-K?sekuchen16LimeLime7Erdbeere-Quark8ErdbErdb8Erdbeere-Mix0ErdbErdb9Jamaica Sun6JamaJama10Waldbeeren0WaldWald1112Sheet1
Which again is wot I expect.
Now I apply this 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:-
ABCDE1Produnt2Name3Chocolate-europe aroma44ChocChoc4Chocolate-Cookies04ChocChoc5Banana-Chocolate-Split104BanaBana6Limette-K?sekuchen164LimeLime7Erdbeere-Quark84ErdbErdb8Erdbeere-Mix04ErdbErdb9Jamaica Sun64JamaJama10Waldbeeren04WaldWald1112Sheet1
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)
P.s. I will also post this Thread Here:
Google first with:
&Short Title or Theme of wot you want&
Use Code Tags: H click on the # sign at the top of the thread window, or Consider using The VB Code HTML Maker and Spreadsheet HTML Maker:
-See bottom of the page (Win & Mac):
XL 10 Not mac
MrExcel MVP
Re: VBA Evaluate Range and VLOOKUP
Interesting question.
First, the way I'd do it would be, for ex., using your formula
With rngCC
.Formula = "=VLOOKUP(" & rngName(1, 1).Address(0, 0) & ",$A$16:$C$33,3,FALSE)"
.Value = .Value
or calling VlookUp() in vba:
rngCC = Application.WorksheetFunction.VLookup(rngName, Range("A16:C33"), 3, False)
Originally Posted by DocAElstein
. 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:
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.
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:
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.
likes this.
Kind regards
To understand recursion, you must understand recursion.
Board Regular
Re: VBA Evaluate Range and VLOOKUP
Re:- VBA Evaluate Range VLOOKUP
Originally Posted by pgc01
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
Google first with:
&Short Title or Theme of wot you want&
Use Code Tags: H click on the # sign at the top of the thread window, or Consider using The VB Code HTML Maker and Spreadsheet HTML Maker:
-See bottom of the page (Win & Mac):
XL 10 Not mac
Board Regular
Re: VBA Evaluate Range and VLOOKUP
Re:- VBA Evaluate Range VLOOKUP
Originally Posted by pgc01
First, the way I'd do it would be, for ex., using your formula
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
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
where the third method kindly suggested by Jerry Sullivan follows very closely your method suggested in the above 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)
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!
Originally Posted by pgc01
?or calling VlookUp() in vba:
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)
? and once again a very helpful comparison. Many Thanks.
ABCDE1Produnt2NameAlanpgcJerry SullivanpgcVBA3Chocolate-europe aroma44444Chocolate-Cookies0005Banana-Chocolate-Split101010106Limette-K?sekuchen161616167Erdbeere-Quark88888Erdbeere-Mix0009Jamaica Sun666610Waldbeeren000Sheet1Worksheet FormulasCellFormulaB3=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
????????????????..
Originally Posted by pgc01
? 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??..
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))")
?. 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?
AB1WAHRText21Sheet2Worksheet FormulasCellFormulaA1=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)
Google first with:
&Short Title or Theme of wot you want&
Use Code Tags: H click on the # sign at the top of the thread window, or Consider using The VB Code HTML Maker and Spreadsheet HTML Maker:
-See bottom of the page (Win & Mac):
XL 10 Not mac
Board Regular
Re: VBA Evaluate Range and VLOOKUP
…………………………………………..
Last Bit again hopefully in correct Form!!
Originally Posted by pgc01
… 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……
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))")
…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?
AB1WAHRText21Sheet2Worksheet FormulasCellFormulaA1=ISTEXT(B1:B3)
………………………….
Originally Posted by pgc01
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)
Google first with:
&Short Title or Theme of wot you want&
Use Code Tags: H click on the # sign at the top of the thread window, or Consider using The VB Code HTML Maker and Spreadsheet HTML Maker:
-See bottom of the page (Win & Mac):
XL 10 Not mac
Board Regular
Re: VBA Evaluate Range and VLOOKUP
Hi again PGC
Originally Posted by pgc01
…...
….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:
ABCDEF1Produnt2Name3Chocolate-europe aroma4Choc4Chocolate-Cookies0Choc5Banana-Chocolate-Split10BanaSheet1Worksheet FormulasCellFormulaB3=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:
Originally Posted by pgc01
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:
={"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…..
Originally Posted by pgc01
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
Google first with:
&Short Title or Theme of wot you want&
Use Code Tags: H click on the # sign at the top of the thread window, or Consider using The VB Code HTML Maker and Spreadsheet HTML Maker:
-See bottom of the page (Win & Mac):
XL 10 Not mac
Board Regular
Re: VBA Evaluate Range and VLOOKUP
Originally Posted by DocAElstein
.........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
Google first with:
&Short Title or Theme of wot you want&
Use Code Tags: H click on the # sign at the top of the thread window, or Consider using The VB Code HTML Maker and Spreadsheet HTML Maker:
-See bottom of the page (Win & Mac):
XL 10 Not mac
MrExcel MVP
Re: VBA Evaluate Range and VLOOKUP
Originally Posted by DocAElstein
. 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!
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:
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
With range
.Formula = someformula
.Value = .Value
... simple and easy to read and understand.
&ABCDEFG1Produnt&&&&&&2Name&&&&&&3Chocolate-europe aroma4&&&Choc&4Chocolate-Cookies0&&&Choc&5Banana-Chocolate-Split10&&&Bana&6&&&&&&& [Book1]Sheet1
AddrFormula[Book1]Sheet1B3: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
likes this.
Kind regards
To understand recursion, you must understand recursion.
Board Regular
Re: VBA Evaluate Range and VLOOKUP
Originally Posted by pgc01
The workaround I was thinking was one published by XOR LX article here:
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!
Advanced Excel Techniques:
MrExcel MVP
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
To understand recursion, you must understand recursion.
Jump to page:

我要回帖

更多关于 如何使用vlookup函数 的文章

 

随机推荐