Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

worksheetfunction.vlookup doesn't work for me.

Posted on 2009-05-11
9
Medium Priority
?
448 Views
Last Modified: 2012-06-27
I am trying to get vlookup to work in VBA for Excel.
I have a line that reads: jjunk = WorksheetFunction.VLookup("Apples", TableThings, 1, False)
junk doesn't matter.  I only care if the vlookup succeeds or fails.
I think I should use error handling which code to execute.  But I can't even find a match if I know there's a match.
In this case I am looking for a match in a table called TableThings on "Apples".
Please help.  I have included my tiny workbook.
David
vlookupProblem.xls
0
Comment
Question by:Cousin86
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24359992
Here is a simiplied version of your code, You dont need error to check it you can do it by other way which is this plus your coding was only looking for apples not for any other value...
Saurabh...

Sub classify()
    Dim xrow As Integer
    
    Dim TableThings As Variant
 
 
 
    For xrow = 4 To 8
        If Application.WorksheetFunction.CountIf(Range("TableThings"), Cells(xrow, 1).Value) > 0 Then
            Cells(xrow, 2) = "Match"
 
 
        Else
            Cells(xrow, 2) = "No Match"
 
        End If
 
 
 
    Next xrow
 
 
 
 
End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24360007
And if you want to do by that way only, as in vlookup then here is the code...
Saurabh...

Sub classify()
    Dim xrow As Integer
    Dim xcol As Integer
    Dim junk As Variant
    Dim TableThings As Variant
 
 
 
    For xrow = 4 To 8
  On Error Resume Next
    junk = WorksheetFunction.VLookup(Cells(xrow, 1).Value, Range("TableThings"), 1, False)
    If Err.Number <> 1004 Then
    Cells(xrow, 2) = "Match"
    Else
    
 
    Cells(xrow, 2) = "No Match"
    Err.Clear
    End If
    
 
    
    Next xrow
 
 
 
 
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:patrickab
ID: 24360212
Cousin86,
There are much easier ways to do it, but sticking with the use of VLOOKUP(), it can be done as in the macro below.
There were all sorts of problems with the code you had adopted, not least of which was that you were attempting to reference the 'TableThings' range without defining it in VBA code. However as you had defined it in the worksheet you caould use that like this:
[TableThings]
The other thing you were doing was to test Apple as being present in the VLOOKUP table on every cycle of the macro For / Next loop. Not surprisingly it found it every time!
Next was the problem that you were not defining the worksheet you were wanting it all to happen in By default it's the active sheet - so it's OK if you're in DATA - but nowhere else.
Anyhow I hope it all helps
Patrick

Sub classify()
Dim xrow As Integer
Dim xcol As Integer
Dim junk As Variant
    
With Sheets("DATA")
    For xrow = 4 To 8
        On Error Resume Next
        junk = Application.WorksheetFunction.VLookup(.Cells(xrow, 1), [TableThings], 1, False)
        If junk = .Cells(xrow, 1) Then
            .Cells(xrow, 2) = "Match"
        Else
            .Cells(xrow, 2) = "No Match"
        End If
    Next xrow
End With
 
End Sub

Open in new window

vlookupProblem-01.xls
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 24361787
Just an additional point on error handling. If you use Application.VLookup rather than WorksheetFunction.VLookup then when there is no match you will get an error value returned (that you can assign to a variant variable and test with IsError) rather than a run-time error that you handle with an On Error statement. For example:
 

Sub classify()
Dim xrow As Long
Dim xcol As Long
Dim junk As Variant
    
With Sheets("DATA")
    For xrow = 4 To 8
        junk = Application.VLookup(.Cells(xrow, 1), [TableThings], 1, False)
        If IsError(junk) Then
            .Cells(xrow, 2) = "No Match"
        Else
            .Cells(xrow, 2) = "Match"
        End If
    Next xrow
End With
 
End Sub

Open in new window

0
 

Author Comment

by:Cousin86
ID: 24369214
I really have learned a lot, not to mention solved my problem.

But could someone explain to me the syntax of the brackets the surround [TableThings].

Thanks so much
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24369527
It's the equivalent of an Evaluate statement. I generally prefer to use Range("TableThings") but that's personal preference.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 24369755
As I understand it, and no doubt someone will correct me, by putting a reference within square brackets, tells the compiler that an address is required or perhaps a pointer to an address - I'm not sure which but I'd guess the former. So by putting a defined range name in square brackets it is translated into a range address. It's a simple way to make use of named ranges. If you use something like this:
Sheets("Sheet1").[A1] = 1234
then [A1] is translated into a range address - 'A1' being the man-readable address.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24372023
It's not just addresses - it will evaluate most things that Application.Evaluate will - for example, this will also work:
[sum(A1:A10)]
 
0
 
LVL 45

Expert Comment

by:patrickab
ID: 24682405
Yep, I since seen the help file on Evaluate. It's clear about [] brackets and that they are the equivalent of 'Evaluate'.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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