Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1157
  • Last Modified:

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,
0
allwork32
Asked:
allwork32
  • 8
  • 5
  • 4
1 Solution
 
dlmilleCommented:
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
 
dlmilleCommented:
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
 
allwork32Author Commented:
Yes, it is in column 1.   thank you.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SiddharthRoutCommented:
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
 
allwork32Author Commented:
here is a examble.  sheet one is primary with the vlookup

Thank you
examble.xls
0
 
SiddharthRoutCommented:
It is just working fine. Just copy the formula all the way down :)

Sid
0
 
allwork32Author Commented:
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
 
SiddharthRoutCommented:
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
 
dlmilleCommented:
@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
 
dlmilleCommented:
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
 
SiddharthRoutCommented:
@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
 
allwork32Author Commented:
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
 
dlmilleCommented:
ok - This is Dave.  It was my code.  I'll take a look now.

Dave
0
 
dlmilleCommented:
@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
 
dlmilleCommented:
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
 
allwork32Author Commented:
Thank you.  I can work with this from here.
0
 
dlmilleCommented:
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 8
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now