Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default use a variable in a formula

I am writing code to add a vlookup function to a column of cells. Each time
the macro is run (because data is added to the spreadsheet), the lookup table
changes in size. The columns and start row don't change, but the end row may
increase. I have a variable LastRow that holds the row number of the end of
the table. How do I incorporate that into my code? Usinge the recorder, I'm
starting with this - I want to change "Sheet4!R2C1:R113C4" to something like
"Sheet4!R2C1:R'LastRow'C4"

ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],Sheet4!R2C1:R113C4,4,0)"
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default use a variable in a formula



Need something like the following



'With Worksheets("Sheet Name")
'set the table for the data you wish to lookup Every time this is run it
should find the last row in column 9
Set Data_TABLE = Range(.Cells(5,1 ), .Cells(Rows.Count, 9).End(xlUp))
'.Select

'the information you wish to find (range "a1")
Find_Value= .cells(1,1) 'a1

'Result is the value to be returned
'explination of VLookup(find thisvalue, in this table, column to lookup,
match type)
Result = Application.VLookup(Find_Value, DATA_Table, 5, 0)

'paste result into ("b1")
..Cells(1,2) = Result

End With





"Marcotte A" wrote in message
...
I am writing code to add a vlookup function to a column of cells. Each
time
the macro is run (because data is added to the spreadsheet), the lookup
table
changes in size. The columns and start row don't change, but the end row
may
increase. I have a variable LastRow that holds the row number of the end
of
the table. How do I incorporate that into my code? Usinge the recorder,
I'm
starting with this - I want to change "Sheet4!R2C1:R113C4" to something
like
"Sheet4!R2C1:R'LastRow'C4"

ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],Sheet4!R2C1:R113C4,4,0)"



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default use a variable in a formula

First you need to find the row number for the last row in
which your data appears.

To find the last row:

rn = Worksheets("name of your sheet").Range("A65536").End
(xlUp).Row

This then assigns the row number to a variable 'rn'. The
formula works by going to the very last row and then going
up to the first row with data in it using column A as the
reference column.

Now amend your formula:

ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],Sheet4!R2C1:R" &
rn &"C4,4,0)"

This should then return the formula with the correct row
number.

BOL
DavidC
-----Original Message-----
I am writing code to add a vlookup function to a column

of cells. Each time
the macro is run (because data is added to the

spreadsheet), the lookup table
changes in size. The columns and start row don't change,

but the end row may
increase. I have a variable LastRow that holds the row

number of the end of
the table. How do I incorporate that into my code?

Usinge the recorder, I'm
starting with this - I want to change "Sheet4!

R2C1:R113C4" to something like
"Sheet4!R2C1:R'LastRow'C4"

ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],Sheet4!

R2C1:R113C4,4,0)"
.

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
DDE formula with variable Bruce Excel Worksheet Functions 0 November 16th 07 09:03 AM
variable in a formula libby Excel Programming 3 August 15th 04 04:31 AM
Variable in a Formula Amy Ja Excel Programming 3 April 21st 04 01:22 AM
Variable in a formula GM[_2_] Excel Programming 6 March 3rd 04 06:27 PM
Variable to formula. russell \(skmr3\) Excel Programming 1 July 30th 03 07:11 AM


All times are GMT +1. The time now is 12:13 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"