ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using variable in formula (https://www.excelbanter.com/excel-programming/427065-re-using-variable-formula.html)

Jacob Skaria

Using variable in formula
 
Variables should be kept out of the string and combined using &..

Public Sub test()
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*" & hv
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Erik" wrote:

Public Sub test()
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*hv"
End Sub

Range("aa1") contains the value 269.1. The value in RC[-1] is 3758.7.

The cell in position 5,23++ does not recognize hv as a number but just shows
"hv" with the rest of the formula. How can I fix this?


Erik

Using variable in formula
 
I get runtime error 1004.


"Jacob Skaria" wrote:

Variables should be kept out of the string and combined using &..

Public Sub test()
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*" & hv
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Erik" wrote:

Public Sub test()
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*hv"
End Sub

Range("aa1") contains the value 269.1. The value in RC[-1] is 3758.7.

The cell in position 5,23++ does not recognize hv as a number but just shows
"hv" with the rest of the formula. How can I fix this?


Patrick Molloy[_2_]

Using variable in formula
 
make sure that you have
OPTION EXPLICIT
heading the code module
ensure that your variables are both correctly dimensioned and that they have
values.

I ran your test, adding a value for i, and it was ok

Public Sub test()
i = 5 ' test to make code work
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*" & hv

End Sub

error 1004 usualy referes to a range not being found

"Erik" wrote:

I get runtime error 1004.


"Jacob Skaria" wrote:

Variables should be kept out of the string and combined using &..

Public Sub test()
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*" & hv
End Sub

If this post helps click Yes
---------------
Jacob Skaria


"Erik" wrote:

Public Sub test()
hv = Range("aa1")
Cells(i + 5, 23).Offset(step, 0).Formula = "=+RC[-1]*52*hv"
End Sub

Range("aa1") contains the value 269.1. The value in RC[-1] is 3758.7.

The cell in position 5,23++ does not recognize hv as a number but just shows
"hv" with the rest of the formula. How can I fix this?



All times are GMT +1. The time now is 05:29 PM.

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