ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA Excel formula error (https://www.excelbanter.com/excel-programming/331246-vba-excel-formula-error.html)

TOMD

VBA Excel formula error
 
Hi,
I'm trying to use a variable in a formula and it's not working. Here is the
formula:

Formula #1
ActiveCell.FormulaR1C1 = "=rc[x]-rc[2]"

The value of x is determined in a select case statement. For example
depending on the case selection I want the formula to say:

Formula #2
ActiveCell.FormulaR1C1 = "rc[-17]-rc[2]"

Formula #2 works but Formula #1 doesn't. How can I use a variable in #1 and
make it work?

Thanks much,
--
TomD

K Dales[_2_]

VBA Excel formula error
 
if x is inside the quotes it is treated as text, not as your variable - do
this instead:
ActiveCell.FormulaR1C1 = "=rc[" & x & "]-rc[2]"

"TomD" wrote:

Hi,
I'm trying to use a variable in a formula and it's not working. Here is the
formula:

Formula #1
ActiveCell.FormulaR1C1 = "=rc[x]-rc[2]"

The value of x is determined in a select case statement. For example
depending on the case selection I want the formula to say:

Formula #2
ActiveCell.FormulaR1C1 = "rc[-17]-rc[2]"

Formula #2 works but Formula #1 doesn't. How can I use a variable in #1 and
make it work?

Thanks much,
--
TomD



All times are GMT +1. The time now is 11:47 PM.

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