Solved

VBA Vlookup

Posted on 2011-03-01
17
1,145 Views
Last Modified: 2012-06-21
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,
0
Comment
Question by:allwork32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 4
17 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 35013125
Not sure why not just put the Excel Vlookup Equation in Column K.  I'm working this for you, now.

Where's the key on Sheet2 - is that column 1 (column A)?

Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35013206
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.
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

Open in new window

Let me know if this helps!


Please see attached demonstration file.

Enjoy!

Dave
LoopingVlookup-r1.xlsm
0
 
LVL 1

Author Comment

by:allwork32
ID: 35013208
Yes, it is in column 1.   thank you.  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35013212
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
0
 
LVL 1

Author Comment

by:allwork32
ID: 35013360
here is a examble.  sheet one is primary with the vlookup

Thank you
examble.xls
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35013364
It is just working fine. Just copy the formula all the way down :)

Sid
0
 
LVL 1

Author Comment

by:allwork32
ID: 35013403
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.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35013452
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
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35013519
@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
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35013528
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
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35013549
@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
0
 
LVL 1

Author Comment

by:allwork32
ID: 35013621
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
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35013627
ok - This is Dave.  It was my code.  I'll take a look now.

Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35013645
@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
0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 35013682
I modified the macro in your exel example based on your changed criteria (at least, it appears changed based on the template you provided - see prior post).

No worries!

see the code, below:  

For Each myCell In Range("B2", Range("B" & Rows.Count).End(xlUp)) - change the B to other column, if you're moving where the vlookup key is in future

and
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:B"), 2, 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, "H").Value = "#VALUE" 'post error message, if any
                On Error GoTo 0
            Else
                Cells(myCell.Row, "H").Value = rngResult
            End If
    Next myCell
End Sub

Open in new window

rngResult = Application.WorksheetFunction.VLookup(myCell.Value, Sheet2.Range("A:B"), 2, 0) ' look for exact match w/ key in column A and result in Column D (4th column)


Change the range ("A:B") to whatever the TABLE is you're looking up to with KEY in first column

Change the ,2, to whatever COLUMN has your data

and the example can be adapted to future use.
 

See attached file.

Dave
Dave
examble-R2.xls
0
 
LVL 1

Author Closing Comment

by:allwork32
ID: 35014615
Thank you.  I can work with this from here.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35014680
If you have future problems or questions about how this was setup, just post here and I'll assist.

Good luck!

Dave
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question