Solved

VBA Vlookup

Posted on 2011-03-01
17
1,137 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
  • 8
  • 5
  • 4
17 Comments
 
LVL 41

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 41

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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 41

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 41

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 41

Expert Comment

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

Dave
0
 
LVL 41

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 41

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 41

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now