Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
DG DG is offline
external usenet poster
 
Posts: 46
Default VLookup Function in VB

Why does this not work:

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA", "A1:D20", 4)
End Sub

I get a Run-time error '1004'
Unable to get the VLookup property of the WorksheetFunction class

Data looks something like this:

Supplier Item Qty Cost
AEARO AEA 123 5 2.50
COBRA COB 123 8 4.50
etc...

DG


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default VLookup Function in VB

DG,

Try it like this:

TESTVAR = WorksheetFunction.VLookup("COBRA", Range("A1:D20"), 4)


--
Hope that helps.

Vergel Adriano


"DG" wrote:

Why does this not work:

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA", "A1:D20", 4)
End Sub

I get a Run-time error '1004'
Unable to get the VLookup property of the WorksheetFunction class

Data looks something like this:

Supplier Item Qty Cost
AEARO AEA 123 5 2.50
COBRA COB 123 8 4.50
etc...

DG



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VLookup Function in VB

Since you're matching on a string, wouldn't you want the match to be exact? I'm
gonna assume that you'd answer yes:

Option Explicit
Sub Test()
dim Res as variant
dim VlookupRng as range
dim WhatWord as string

set vlookuprng = worksheets("dataquery").range("a1:d20")

whatword = "Cobra"

res = application.vlookup(whatword, vlookuprng, 4, false)

if iserror(res) then
msgbox "No match!"
else
msgbox res
end if

End Sub

(Untested, uncompiled)

You could have used:

res = application.vlookup("cobra", worksheets("dataquery").range("a1:d20"), _
4, false)


DG wrote:

Why does this not work:

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA", "A1:D20", 4)
End Sub

I get a Run-time error '1004'
Unable to get the VLookup property of the WorksheetFunction class

Data looks something like this:

Supplier Item Qty Cost
AEARO AEA 123 5 2.50
COBRA COB 123 8 4.50
etc...

DG


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default VLookup Function in VB

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA",
Range("A1:D20"), 4)
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"DG" wrote in message
...
Why does this not work:

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA", "A1:D20", 4)
End Sub

I get a Run-time error '1004'
Unable to get the VLookup property of the WorksheetFunction class

Data looks something like this:

Supplier Item Qty Cost
AEARO AEA 123 5 2.50
COBRA COB 123 8 4.50
etc...

DG



  #5   Report Post  
Posted to microsoft.public.excel.programming
DG DG is offline
external usenet poster
 
Posts: 46
Default VLookup Function in VB

I tried that and got the same error message.

That's when I put the Sheets("DataQuery").Select statement in thinking that
it didn't have the focus on any sheet. But still getting the same error.

DG

"Vergel Adriano" wrote in message
...
DG,

Try it like this:

TESTVAR = WorksheetFunction.VLookup("COBRA", Range("A1:D20"), 4)


--
Hope that helps.

Vergel Adriano


"DG" wrote:

Why does this not work:

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA", "A1:D20", 4)
End Sub

I get a Run-time error '1004'
Unable to get the VLookup property of the WorksheetFunction class

Data looks something like this:

Supplier Item Qty Cost
AEARO AEA 123 5 2.50
COBRA COB 123 8 4.50
etc...

DG







  #6   Report Post  
Posted to microsoft.public.excel.programming
DG DG is offline
external usenet poster
 
Posts: 46
Default VLookup Function in VB

You are correct I did want it to be exact. I haven't tried the long version
but your second example worked.

Thanks.

"Dave Peterson" wrote in message
...
Since you're matching on a string, wouldn't you want the match to be
exact? I'm
gonna assume that you'd answer yes:

Option Explicit
Sub Test()
dim Res as variant
dim VlookupRng as range
dim WhatWord as string

set vlookuprng = worksheets("dataquery").range("a1:d20")

whatword = "Cobra"

res = application.vlookup(whatword, vlookuprng, 4, false)

if iserror(res) then
msgbox "No match!"
else
msgbox res
end if

End Sub

(Untested, uncompiled)

You could have used:

res = application.vlookup("cobra",
worksheets("dataquery").range("a1:d20"), _
4, false)


DG wrote:

Why does this not work:

Sub TEST()
Dim TESTVAR As Integer
Sheets("DataQuery").Select
TESTVAR = Application.WorksheetFunction.VLookup("COBRA", "A1:D20", 4)
End Sub

I get a Run-time error '1004'
Unable to get the VLookup property of the WorksheetFunction class

Data looks something like this:

Supplier Item Qty Cost
AEARO AEA 123 5 2.50
COBRA COB 123 8 4.50
etc...

DG


--

Dave Peterson



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to combine Combo Box function with Vlookup function KH Excel Worksheet Functions 2 April 5th 10 01:24 PM
Combine VLOOKUP and IF function so #NA isn't returned as a value from VLOOKUP buffgirl71 Excel Discussion (Misc queries) 12 November 14th 06 11:36 PM
HOW DO I NEST THE VLOOKUP FUNCTION WITH THE LEFT FUNCTION CHAIM Excel Worksheet Functions 1 July 27th 05 09:10 PM
how do I write a vlookup function within an iserror function so t. JBLeeds Excel Worksheet Functions 2 March 16th 05 10:30 AM
I want to use Vlookup function and AND function in a single formu. prakash Excel Worksheet Functions 3 January 25th 05 07:11 AM


All times are GMT +1. The time now is 03:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"