Cousin86
asked on
worksheetfunction.vlookup doesn't work for me.
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
I have a line that reads: jjunk = WorksheetFunction.VLookup(
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
And if you want to do by that way only, as in vlookup then here is the code...
Saurabh...
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
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
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
vlookupProblem-01.xls
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
But could someone explain to me the syntax of the brackets the surround [TableThings].
Thanks so much
It's the equivalent of an Evaluate statement. I generally prefer to use Range("TableThings") but that's personal preference.
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.
Sheets("Sheet1").[A1] = 1234
then [A1] is translated into a range address - 'A1' being the man-readable address.
It's not just addresses - it will evaluate most things that Application.Evaluate will - for example, this will also work:
[sum(A1:A10)]
[sum(A1:A10)]
Yep, I since seen the help file on Evaluate. It's clear about [] brackets and that they are the equivalent of 'Evaluate'.
Saurabh...
Open in new window