ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   VLookUp in VBA (https://www.excelbanter.com/excel-discussion-misc-queries/192668-vlookup-vba.html)

PeterM

VLookUp in VBA
 
Using Excel 2003, is there a way to use the =Vlookup worksheet function in a
macro? (VBA code). I've tried

dim resultt as string
resultt=vlookup("02111","a1:ab545",3,false)

and all I get is a "sub or function not defined" message.

Thanks in advance for your help.

Dave Peterson

VLookUp in VBA
 
Dim Res as variant 'could be an error
dim myVal as variant 'string or long or double or ???
dim LookUpRng as range

with activesheet
set lookuprng = .range("a1:ab545")
end with

myVal = "02111" 'not 2111???

res = application.vlookup(myval, lookuprng, 3, false)

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



PeterM wrote:

Using Excel 2003, is there a way to use the =Vlookup worksheet function in a
macro? (VBA code). I've tried

dim resultt as string
resultt=vlookup("02111","a1:ab545",3,false)

and all I get is a "sub or function not defined" message.

Thanks in advance for your help.


--

Dave Peterson

PeterM

VLookUp in VBA
 
thanks for responding

"02111" is in quotes because in the worksheet the value to be looked up is
number stored as a text string, therefore "02111"

but you've answered my question, got it to work....thank you for your help

"Dave Peterson" wrote:

Dim Res as variant 'could be an error
dim myVal as variant 'string or long or double or ???
dim LookUpRng as range

with activesheet
set lookuprng = .range("a1:ab545")
end with

myVal = "02111" 'not 2111???

res = application.vlookup(myval, lookuprng, 3, false)

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



PeterM wrote:

Using Excel 2003, is there a way to use the =Vlookup worksheet function in a
macro? (VBA code). I've tried

dim resultt as string
resultt=vlookup("02111","a1:ab545",3,false)

and all I get is a "sub or function not defined" message.

Thanks in advance for your help.


--

Dave Peterson



All times are GMT +1. The time now is 08:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com