View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bimal[_3_] Bimal[_3_] is offline
external usenet poster
 
Posts: 6
Default Help with VBA Vlookup

Dave,
Its not true. When I came across this error, I make sure to use only
those value where there is a match. Moreover, if there is no match,
the value will not appear in the combobox.
Its nice to learn about the difference. Now I have tried with both
but without success.
While using only application.vlookup, I got run-time error
'-2147352571(80020005)' Could not set the value property, Type
mismatch.
This is new error. Does it mean something? If I use the vlookup in one
of the cell, instead of VBA, for the same value, it works.
Regards,
Bimal

Dave Peterson wrote in message ...
I bet there isn't a match for your vlookup.

When I work with worksheet functions inside VBE, I (almost) always drop the
.worksheetfunction portion.

There's a few functions where application.worksheetfunction.xxx and
application.xxx handle things differently.

Two of them are .match and .vlookup.

Used like application.worksheetfunction.vlookup(), if no match is found, then an
error is raised:

dim Res as variant
on error resume next
res = application.worksheetfunction.vlookup(...)
if err.num < 0 then
'no match found
err.clear
end if
on error goto 0


But if you use it like: application.vlookup(), an error can be returned:

dim res as variant
res = application.vlookup(...)
if iserror(res) then
'no match found
end if

I find the second version easier.

=============

So in your case:

dim res as variant
res = Application.WorksheetFunction.Vlookup(ComboBox1.Va lue, _
Sheets("Out").Range("B:M"), 6, False)

if iserror(res) then
txcon.value = "No match!"
else
txcon.value = res
end if

(untested, so watch for typos!)

Bimal wrote:

Hi guys,
I was using vlookup in a combobox.click event to populate the text box
as per the value of combobox. I came across a rather strange behaviour
of VBA.
The problem line is
Txcon.Value = Application.WorksheetFunction.Vlookup(ComboBox1.Va lue,
Sheets("Out").Range("B:M"), 6, False)

Txcon is textbox. This gives run-time error 1004 "Unable to get the
vlookup property of the worksheetfunction class".

However the same line works fine if I change the sheet name from "Out"
to "In"

I cant belive there is my mistake coz it works properly if change the
sheet name.
Is this a bug or something else?

Hope some expert can answer me.
Bimal.