Using a variable in formula

rguy84

Board Regular
Joined
May 18, 2011
Messages
112
How do you use a variable in a formula?

I have a variable:
Code:
lastRow = Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row

I have a function called CalcTime(start,end), and I need to insert that into mlastRow. I am attempting
Code:
Range("M" & lastRow).Formula = "=CalcTime("K"&lastRow,"L"&lastRow)"
I either get an undefined error or the cell is =CalcTime(KlastRow,LlastRow) versus =Calctime(K4,L4) depending where and how many quotes I add
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Yours looks like too many " jonmo

Code:
Range("M" & lastRow).Formula = "=CalcTime(K" & lastRow &",L" & lastRow &")"

or maybe

Code:
Range("M" & lastRow).FormulaR1C1 = "=CalcTime(RC11,RC12)"
 
Upvote 0
Those are ranges specified in R1C1 reference style.

RC11 translates to same row, column 11
RC12 translates to same row, column 12

in short, if the formula is written to M4, then it will come out as =Calctime(K4,L4)
if it's written to M20 then it will come out as =CalcTime(K20,L20)

Same logic applies anywhere, the row used in the formula will be the same as the row holding the formula.
 
Upvote 0
haha nice. Almost like cheating. Out of curiousity why does it put $k12 vs k12? Cause technically to reference a cell you do $a$1?
 
Upvote 0
Just the way it translates between the 2 reference styles

R defines the row, C defines the column, a number after the row or column defines it as absolute (i.e. with a $ prefix)

No number, defines it as relative, using the row or column holding the formula.

Numbers in [] act as an offset relative to the cell holding the formula.

R[-1]C[2] means 1 row above, 2 columns right (negative numbers offset left or up, positive down or right).

As with A1 references, you can mix the absolute and relative parts of the ranges, which is what happens when you write the formula with vba, it would appear to use the most relevant format based on how the range is specified.

Maybe jonmo will be kind enough to elaborate on any points I may have missed / mis-described.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top