Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default vlookup function in vba

I want to use a vlookup functin in vba

Range("a15") = "=vlookup(d12,a1:b6,2,false)"
this works ok.

But can I use a VARIABLE in lookup_value (i.e. instead of D12)
I am giving a sample code more to clarify the point to myself

code begins
Dim datarange As Range
Dim myrange As Range
Set datarange = Range("a1:B6")
'(a few of the cells from the col A of datarange is "myrange")
Set myrange = Range("d12:d14")
For Each c In myrange
c.Offset(0, 1) = "=vlookup(c,datarange,2,false)"
Next
code ends
this does not give the desired result . c.offset(0,1) values become
0(zero). where is the error in my logic. is it something to do with the
vlookup function being a worksheet function and not a vba function. I even
used
c.address or even c.value instead of c with no success(the results are
#name?). does it mean I
cannot use a variable in vlookup function.
Of course I have designed the code in some other way without using the
variable for lookup_value to get the desired result.






  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default vlookup function in vba

R.Venkataraman,

If you want the addresses, the following works-

c.Offset(0, 1) = "=vlookup(" & c.Address & "," & datarange.Address &
",2,false)"

If you want the value of "c", the following work (a) Numeric values (b)
Alphanumeric or text values -

a) c.Offset(0, 1) = "=vlookup(" & c.Value & "," & datarange.Address &
",2,false)"

b) c.Offset(0, 1) = "=vlookup(""" & c.Value & """," & datarange.Address &
",2,false)"

regards,

JohnI

"R.Venkataraman" wrote in message
...
I want to use a vlookup functin in vba

Range("a15") = "=vlookup(d12,a1:b6,2,false)"
this works ok.

But can I use a VARIABLE in lookup_value (i.e. instead of D12)
I am giving a sample code more to clarify the point to myself

code begins
Dim datarange As Range
Dim myrange As Range
Set datarange = Range("a1:B6")
'(a few of the cells from the col A of datarange is "myrange")
Set myrange = Range("d12:d14")
For Each c In myrange
c.Offset(0, 1) = "=vlookup(c,datarange,2,false)"
Next
code ends
this does not give the desired result . c.offset(0,1) values become
0(zero). where is the error in my logic. is it something to do with the
vlookup function being a worksheet function and not a vba function. I

even
used
c.address or even c.value instead of c with no success(the results are
#name?). does it mean I
cannot use a variable in vlookup function.
Of course I have designed the code in some other way without using the
variable for lookup_value to get the desired result.








  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default vlookup function in vba

This will leave the value of the formula at the offset. No need to clutter
up with formulas.

For Each c In Range("d12:d14")
c.Offset(, 1) = Application.VLookup(c, Range("a1:b6"), 2, 0)
Next

"R.Venkataraman" wrote in message
...
I want to use a vlookup functin in vba

Range("a15") = "=vlookup(d12,a1:b6,2,false)"
this works ok.

But can I use a VARIABLE in lookup_value (i.e. instead of D12)
I am giving a sample code more to clarify the point to myself

code begins
Dim datarange As Range
Dim myrange As Range
Set datarange = Range("a1:B6")
'(a few of the cells from the col A of datarange is "myrange")
Set myrange = Range("d12:d14")
For Each c In myrange
c.Offset(0, 1) = "=vlookup(c,datarange,2,false)"
Next
code ends
this does not give the desired result . c.offset(0,1) values become
0(zero). where is the error in my logic. is it something to do with the
vlookup function being a worksheet function and not a vba function. I

even
used
c.address or even c.value instead of c with no success(the results are
#name?). does it mean I
cannot use a variable in vlookup function.
Of course I have designed the code in some other way without using the
variable for lookup_value to get the desired result.








  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default vlookup function in vba

thanks both of you.

"Don Guillett" wrote in message
...
This will leave the value of the formula at the offset. No need to clutter
up with formulas.

For Each c In Range("d12:d14")
c.Offset(, 1) = Application.VLookup(c, Range("a1:b6"), 2, 0)
Next

"R.Venkataraman" wrote in message
...
I want to use a vlookup functin in vba

Range("a15") = "=vlookup(d12,a1:b6,2,false)"
this works ok.

But can I use a VARIABLE in lookup_value (i.e. instead of D12)
I am giving a sample code more to clarify the point to myself

code begins
Dim datarange As Range
Dim myrange As Range
Set datarange = Range("a1:B6")
'(a few of the cells from the col A of datarange is "myrange")
Set myrange = Range("d12:d14")
For Each c In myrange
c.Offset(0, 1) = "=vlookup(c,datarange,2,false)"
Next
code ends
this does not give the desired result . c.offset(0,1) values become
0(zero). where is the error in my logic. is it something to do with the
vlookup function being a worksheet function and not a vba function. I

even
used
c.address or even c.value instead of c with no success(the results are
#name?). does it mean I
cannot use a variable in vlookup function.
Of course I have designed the code in some other way without using the
variable for lookup_value to get the desired result.










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 09:57 AM.

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"