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 ?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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:
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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