Is it possible to use variable in brackets when using .FormulaR1C1 ?

ltirole

New Member
Joined
Aug 2, 2018
Messages
2
Hello,
I´m new to VBA and I wonder if you can put a variable inside the brackets when using .FormulaR1C1.
Here is an example of what I´m trying to do:
Code:
Sub b()


    Dim fl As Integer
    Dim fc As Integer
    Dim nb_parts As Integer
    Dim nb_measurments As Integer
    fl = 8
    fc = 8
    nb_measurments = 7
    nb_parts = 30
    
    
    For x = 0 To nb_measurments - 1
    Cells(fl + nb_parts + 4, fc + x).FormulaR1C1 = "=AVERAGE(R[-(nb_parts)]C:R[-4]C)" 'here i get run time error 1004
    Next x
End Sub

Do you have a solution ?
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Fazza

MrExcel MVP
Joined
May 17, 2006
Messages
9,368
nb_parts is just part of what is a text string where you're getting the error. it makes no sense, but i guess you know that

a common approach is ="AVERAGE(R[-" & nb_parts & "]C:R[-4]C)"

another way would be to replace the string by its value
=REPLACE$("AVERAGE(R[-(nbparts)]C:R[-4]C)","nb_parts",nb_parts)

HTH
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,894
Messages
5,598,724
Members
414,253
Latest member
MarieCo

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
Top