Jerome Hayes
asked on
VBA Vlookup
Experts,
Can someone help me with this code. I would like to look at contents in column B on sheet 1 and put a vlookup in corresponding cell in column K which takes data from sheet2. column 4. I'm able to do the vlookup but not able to get it to loop through column B on sheet1 and apply vlookup to coresponding cell in column K.
thank you,
Can someone help me with this code. I would like to look at contents in column B on sheet 1 and put a vlookup in corresponding cell in column K which takes data from sheet2. column 4. I'm able to do the vlookup but not able to get it to loop through column B on sheet1 and apply vlookup to coresponding cell in column K.
thank you,
Attached, please find a looping vlookup routine. Progresses through column B used range, uses that value as the KEY to the Vlookup, with TABLE coming from Sheet2 (columns 1-4), and value to return at column 4. If there's an error, then #VALUE would be placed in column K, same row as key.
Please see attached demonstration file.
Enjoy!
Dave
LoopingVlookup-r1.xlsm
Sub DoVlookupsPasteValues()
Dim myCell As Range
Dim rngResult As Variant
For Each myCell In Range("B2", Range("B" & Rows.Count).End(xlUp))
Debug.Print myCell.Address
On Error Resume Next
rngResult = Application.WorksheetFunction.VLookup(myCell.Value, Sheet2.Range("A:D"), 4, 0) ' look for exact match w/ key in column A and result in Column D (4th column)
If Err.Number <> 0 Then
Cells(myCell.Row, "K").Value = "#VALUE" 'post error message, if any
On Error GoTo 0
Else
Cells(myCell.Row, "K").Value = rngResult
End If
Next myCell
End Sub
Let me know if this helps!Please see attached demonstration file.
Enjoy!
Dave
LoopingVlookup-r1.xlsm
ASKER
Yes, it is in column 1. thank you.
There could be many reasons for it
The most common error is that the The Lookup Range is not encapsulated in "$" so that the range changes every time for example see below
=Vlookup(B1, D1:D4, 2, 0)
When you copy the above formula down it will not work. The correct way of doing it is as below.
=Vlookup(B1, $D$1:$D$4, 2, 0)
Would definitely like to see a sample file before I can comment more.
Sid
The most common error is that the The Lookup Range is not encapsulated in "$" so that the range changes every time for example see below
=Vlookup(B1, D1:D4, 2, 0)
When you copy the above formula down it will not work. The correct way of doing it is as below.
=Vlookup(B1, $D$1:$D$4, 2, 0)
Would definitely like to see a sample file before I can comment more.
Sid
ASKER
It is just working fine. Just copy the formula all the way down :)
Sid
Sid
ASKER
Hello Sid,
In stead of copying the formulat all the way down, i wanted VBA code to loop through column B and apply the Vlookup formula in column K. Is this possible or should I just stick to copying it down.
In stead of copying the formulat all the way down, i wanted VBA code to loop through column B and apply the Vlookup formula in column K. Is this possible or should I just stick to copying it down.
VBA code can do that for you. But it will be an Overkill for this situation.
Are you aware the if you hover the mouse on the bottom right corner of the cell, your cursor will change to a "+" sign. If you double click, you don't even need to drag the formula down. It will automatically do that for you.
However if you still need a vba code do let me know and I will write one for you :)
Sid
Are you aware the if you hover the mouse on the bottom right corner of the cell, your cursor will change to a "+" sign. If you double click, you don't even need to drag the formula down. It will automatically do that for you.
However if you still need a vba code do let me know and I will write one for you :)
Sid
@allwork - it certainly is possible - I posted your solution almost an hour ago!
See this post, just above, or this link: http:\\35013206.html
Please advise if you've tested it as it works just fine from where I sit.
Dave
See this post, just above, or this link: http:\\35013206.html
Please advise if you've tested it as it works just fine from where I sit.
Dave
Sorry - my link doesn't work.
Just look at my post right after your question was asked. I spent time and effort on this, have you spent anytime reviewing my solution?
Dave
Just look at my post right after your question was asked. I spent time and effort on this, have you spent anytime reviewing my solution?
Dave
@allwork: Please ignore the line
However if you still need a vba code do let me know and I will write one for you :)
from my previous post. David has already written the code for you.
David: Sorry abt that...
Sid
However if you still need a vba code do let me know and I will write one for you :)
from my previous post. David has already written the code for you.
David: Sorry abt that...
Sid
ASKER
Hello Siddharth, I apologize for overlooking your code. I tried your code and it didn't work for what I am trying to do. If possible, please apply the code to my example file.
Thank you,
J
Thank you,
J
ok - This is Dave. It was my code. I'll take a look now.
Dave
Dave
@allwork - your example file is NOT how you described it in the original question.
>>Can someone help me with this code. I would like to look at contents in column B on sheet 1 and put a vlookup in corresponding cell in column K which takes data from sheet2. column 4. I'm able to do the vlookup but not able to get it to loop through column B on sheet1 and apply vlookup to coresponding cell in column K.
Should I adapt to your example file?
E.g., look at the contents in column B sheet 1 - ok that matches
put vlookup in corresponding cell in column K - you show the vlookup in column H. Where do you want it, please? :)
takes data from sheet2.column 4 - you don't have any data in column 4, your data appears to be in column 2. What would you like?
Dave
>>Can someone help me with this code. I would like to look at contents in column B on sheet 1 and put a vlookup in corresponding cell in column K which takes data from sheet2. column 4. I'm able to do the vlookup but not able to get it to loop through column B on sheet1 and apply vlookup to coresponding cell in column K.
Should I adapt to your example file?
E.g., look at the contents in column B sheet 1 - ok that matches
put vlookup in corresponding cell in column K - you show the vlookup in column H. Where do you want it, please? :)
takes data from sheet2.column 4 - you don't have any data in column 4, your data appears to be in column 2. What would you like?
Dave
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. I can work with this from here.
If you have future problems or questions about how this was setup, just post here and I'll assist.
Good luck!
Dave
Good luck!
Dave
Where's the key on Sheet2 - is that column 1 (column A)?
Dave